结果传递失败 ResultInfo{who=null, request=100, result=-1, data=Intent

Failure delivering result ResultInfo{who=null, request=100, result=-1, data=Intent

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

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.access00(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 时出现此异常。

尝试改变这个

if (resultCode == RESULT_OK) {

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

Caused by java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.contains(java.lang.CharSequence)' on a null object reference

这是您实现问题的主要异常。

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

问题就在这里:

selectedVideoUri = data.getData();

videoView.setVideoURI(selectedVideoUri);

因为你的数据是空的。