【问题标题】:Failure delivering result ResultInfo{who=null, request=100, result=-1, data=Intent传递结果失败 ResultInfo{who=null, request=100, result=-1, data=Intent
【发布时间】:2018-08-22 09:38:33
【问题描述】:

从图库中选择视频时,我很少遇到异常-

Fatal Exception: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=100, result=-1, data=Intent { dat=content://com.estrongs.files/storage/emulated/0/ADM/720P_1500K_152999072.mp }} to activity {xyzMainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.contains(java.lang.CharSequence)' on a null object reference
       at android.app.ActivityThread.deliverResults(ActivityThread.java)
       at android.app.ActivityThread.handleSendResult(ActivityThread.java)
       at android.app.ActivityThread.access$1500(ActivityThread.java)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java)
       at android.os.Handler.dispatchMessage(Handler.java)
       at android.os.Looper.loop(Looper.java)
       at android.app.ActivityThread.main(ActivityThread.java)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
       at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:102)

Caused by java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.contains(java.lang.CharSequence)' on a null object reference
       at android.content.ContentResolver.checkLeakDetectIgnoreList(ContentResolver.java)
       at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java)
       at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java)
       at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java)
       at android.media.MediaPlayer.setDataSource(MediaPlayer.java)
       at android.widget.VideoView.openVideo(VideoView.java)
       at android.widget.VideoView.setVideoURI(VideoView.java)
       at android.widget.VideoView.setVideoURI(VideoView.java)
       at xyz.onActivityResult(Unknown Source)
       at android.app.Activity.dispatchActivityResult(Activity.java)
       at android.app.ActivityThread.deliverResults(ActivityThread.java)

下面是我正在使用的代码:

private void uploadVideo() {
    try {
        Intent intent = new Intent();
        intent.setType("video/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(Intent.createChooser(intent, "Select Video"), REQUEST_TAKE_GALLERY_VIDEO);
    } catch (Exception e) {

    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK) {
        if (requestCode == REQUEST_TAKE_GALLERY_VIDEO) {
            selectedVideoUri = data.getData();
            videoView.setVideoURI(selectedVideoUri);
            videoView.start();
        }
    }
}

在访问数据之前,我正在检查onActivityResult 中的resultCode,因此无法理解为什么会出现此异常。

【问题讨论】:

  • 哪个例外?你至少有两个! NullPointerException.
  • @greenapps 我得到了两个..第一个由第二个引起
  • Google 是你的朋友。
  • while uploading video from gallery-。上传?我没有看到上传的代码。
  • @greenapps 在问题中更新..我的意思是从图库中选择视频并上传到 videoview

标签: android android-intent nullpointerexception android-implicit-intent


【解决方案1】:

尝试改变这个

if (resultCode == RESULT_OK) {

 if (resultCode == RESULT_OK && data.getData()!=null) {

【讨论】:

  • 应该是 'data!=null' 还是 'data.getData()!=null'?
  • @varmashrivastava 两者都试
  • 如果数据为空,那么为什么会显示异常-data=Intent { dat=content://com.estrongs.files/storage/emulated/0/ADM/720P_1500K_152999072.mp }} to activity {xyzMainActivity}:
【解决方案2】:

由 java.lang.NullPointerException 引起:尝试在空对象引用上调用虚拟方法 'boolean java.lang.String.contains(java.lang.CharSequence)'

这是你意识到问题的主要例外。

在 android.widget.VideoView.setVideoURI(VideoView.java)

问题就在这里:

selectedVideoUri = data.getData();

videoView.setVideoURI(selectedVideoUri);

因为你的数据是空的。

【讨论】:

  • 如何防止异常?我应该更新到if (resultCode == RESULT_OK && data!=null) 吗?
  • 是的,你应该添加对 null 的检查以防止 null 引用。
  • smth like data != null
  • 如果数据为空,那么为什么会显示异常-data=Intent { dat=content://com.estrongs.files/storage/emulated/0/ADM/720P_1500K_152999072.mp }} to activity {xyzMainActivity}:
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多