videoview.setVideoUri 显示空指针异常

videoview.setVideoUri shows null pointer exception

所以我有一个 VideoView 但是当我尝试播放视频时(在我的 res/raw 文件夹中)它给了我空指针异常。我尝试使用 video.setVideoUri(uri)video.setVideoPath(path) 但它仍然显示空指针异常。

Activity:

public class Player extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        VideoView videoView=findViewById(R.id.videoView);
        String path="android.resource://"+getPackageName()+"/"+R.raw.playthis;
        Uri uri=Uri.parse(path);
        videoView.setVideoPath(uri.getPath());
    }

    public void goBackVideo(View view){
        finish();
    }
}

xml 对于 VideoView:

<?xml version="1.0" encoding="utf-8"?>

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="match_parent"
             android:layout_width="match_parent"
             android:id="@+id/VideoPlayerFrameLayout" android:background="@drawable/player_video_pause">
    <VideoView android:layout_width="match_parent" android:layout_height="match_parent"
               android:id="@+id/videoView"/>
    <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content"
                 android:layout_gravity="center_vertical|center_horizontal"
                 android:src="@drawable/player_icon_play" android:onClick="setPlayORpause"
                 android:id="@+id/PauseButton" android:background="#fff"/>

    <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content"
                 android:layout_gravity="left|center" android:layout_marginLeft="160dp"
                 android:src="@drawable/player_icon_prev" android:id="@+id/PrevButton" android:background="#fff"/>

    <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content"
                 android:layout_gravity="right|center" android:layout_marginRight="160dp"
                 android:src="@drawable/player_icon_next" android:id="@+id/NextButton" android:background="#fff"/>

    <ImageButton
            android:layout_width="46dp"
            android:layout_height="34dp"
            android:id="@+id/backPressButton"
            android:rotation="180" android:backgroundTint="#fff"
            android:background="@null"
            android:src="@drawable/settings_icon_back"
            android:layout_gravity="left"
            android:onClick="goBackVideo" android:drawingCacheQuality="high"/>
</FrameLayout>

Logcat:

2020-10-29 06:58:50.965 13640-13640/com.testapp.test3 E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.testapp.test3, PID: 13640
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.testapp.test3/com.testapp.test3.VideoPlayer}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.VideoView.setVideoPath(java.lang.String)' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2887)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2965)
        at android.app.ActivityThread.-wrap11(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1669)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:192)
        at android.app.ActivityThread.main(ActivityThread.java:6702)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:549)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:826)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.VideoView.setVideoPath(java.lang.String)' on a null object reference
        at com.testapp.test3.VideoPlayer.onCreate(VideoPlayer.java:26)
        at android.app.Activity.performCreate(Activity.java:7115)
        at android.app.Activity.performCreate(Activity.java:7106)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1220)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2840)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2965) 
        at android.app.ActivityThread.-wrap11(Unknown Source:0) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1669) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:192) 
        at android.app.ActivityThread.main(ActivityThread.java:6702) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:549) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:826) 

我是否需要先设置内容提供商才能播放任何视频? 我该如何解决这个问题?

编辑: 现在我得到另一个错误

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_player);
        final VideoView videoView=findViewById(R.id.videoView);
        Uri uri=Uri.parse("content://" +"sdcard/"+Environment.DIRECTORY_DOWNLOADS+"/"+"playthis.mp4");
        MediaController mediaController=new MediaController(this);
        videoView.setMediaController(mediaController);
        mediaController.setAnchorView(videoView);
        videoView.setVideoURI(uri);

        videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                videoView.seekTo(0);
                videoView.start();

            }

        });

    }

logcat:

2020-10-29 07:51:39.371 27420-27420/com.testapp.test3 E/ActivityThread: Failed to find provider info for sdcard
2020-10-29 07:51:39.371 27420-27420/com.testapp.test3 W/MediaPlayer: Couldn't open content://sdcard/Download/playthis.mp4: java.io.FileNotFoundException: No content provider: content://sdcard/Download/playthis.mp4
2020-10-29 07:51:39.375 27420-27420/com.testapp.test3 E/MediaPlayerNative: Unable to create media player
2020-10-29 07:51:39.381 27420-27420/com.testapp.test3 W/VideoView: Unable to open content: content://sdcard/Download/playthis.mp4
    java.io.IOException: setDataSource failed.: status=0x80000000
        at android.media.MediaPlayer.nativeSetDataSource(Native Method)
        at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1181)
        at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1169)
        at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1087)
        at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1108)
        at android.widget.VideoView.openVideo(VideoView.java:399)
        at android.widget.VideoView.-wrap0(Unknown Source:0)
        at android.widget.VideoView.surfaceCreated(VideoView.java:678)
        at android.view.SurfaceView.updateSurface(SurfaceView.java:710)
        at android.view.SurfaceView.onPreDraw(SurfaceView.java:145)
        at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:977)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2427)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1446)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6954)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:925)
        at android.view.Choreographer.doCallbacks(Choreographer.java:737)
        at android.view.Choreographer.doFrame(Choreographer.java:669)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:911)
        at android.os.Handler.handleCallback(Handler.java:790)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:192)
        at android.app.ActivityThread.main(ActivityThread.java:6702)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:549)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:826)

你应该在你的 activity onCreate 上添加一个 contentView 所以改变你的 onCreate 像这样:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.your_layout_name)  // Add this line, 
        VideoView videoView=findViewById(R.id.videoView);
        String path="android.resource://"+getPackageName()+"/"+R.raw.playthis;
        Uri uri=Uri.parse(path);
        videoView.setVideoPath(uri.getPath());
    }

之后你应该检查null视频路径,比如

// If path is not null then set it to your videoView.
if(uri.getPath() != null) {
    videoView.setVideoPath(uri.getPath());
}