0

我一直在尝试创建一个显示 VideoView 的应用程序,在它下面有三个按钮,如此处所示video.xml,它是Relative Layout.

<Button
    android:id="@+id/button1"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:text="Choice 1"
    android:onClick="choice1" />

<Button
    android:id="@+id/button2"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/button1"
    android:layout_alignBottom="@+id/button1"
    android:layout_centerHorizontal="true"
    android:text="Choice 2" 
    android:onClick="choice2"/>

<Button
    android:id="@+id/button3"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:text="Choice 3" />

<VideoView
    android:id="@+id/videoView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/button1"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:onClick="choice3" />

然后,我在MethodVideoActivity.java下设置了以下源代码。onCreate

View video = (VideoView) findViewById(R.id.videoView1);
    MediaController mediaController = new MediaController(this);
    mediaController.setAnchorView(video);
    ((VideoView) video).setMediaController(mediaController);
    video.setKeepScreenOn(true);
     Uri video1 = Uri.parse("android.resource://" + getPackageName() + "/"
                + R.raw.start);
              VideoView videoHolder = null;
            videoHolder.setVideoURI(video1);
            ((VideoView) video).start();
            video.requestFocus();

这是 LogCat 报告

I/ActivityManager( 149): Starting: Intent {cmp=com.apw.games.rpg.medieval.silver/.VideoActivity } from pid 2408
I/WindowManager( 149): Setting rotation to 1,
animFlags=1
I/ActivityManager( 149): Config changed: {scale=1.0 imsi 310/4 loc=en US touch=3 keys=2/1/1 nav=2/1 orien=2 layout-18 uiMode=17 seq=37}
DQuickSettings( 239): setGPSButton()
W/dalvikvm( 2408): threadid=1: thread exiting with uncaught exception (group=0x40018560_
E/AndroidRuntime( 2408): FATAL_EXCEPTION: main
E/AndroidRuntime( 2408): java.lang.RuntimeException:
Unable to start activity      ComponentInfo{com.apw.games.rpg.medieval.silver/com.apw.games.rpg.medieval.silver.VideoActivity}: java.lang.NullPointerException
E/AndroidRuntime( 2408): at Android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1658)
E/ANdroidRuntime( 2408): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1674)
E/AndroidRuntime( 2408): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
E/AndroidRuntime( 2408): at android.app.ActivityThread$H.handle.Message(ActivityThread.java931)
E/AndroidRuntime( 2408): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 2408): at android.os.Looper.loop(Looper.java:130)
E/AndroidRuntime( 2408): at android.app.ActivityThread.main(ActivityThread.java:3694)
E/AndroidRuntime( 2408): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 2408): at java.lang.reflect.Method.invoke(Method.java.507)
E/AndroidRuntime( 2408): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
E/AndroidRuntime( 2408): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:662)
E/AndroudRuntime( 2408): at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime( 2408): Caused by java.lang.NullPointerException
E/AndroidRuntime( 2408): at com.apw.games.rpg.medieval.silver.VideoActivity.onCreate(VideoActivity.java)
E/AndroidRuntime( 2408): at android.app.Intrumentation.callActivityOnCreate(Instrumentation.java.1047)
E/AndroidRuntime( 2408): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
4

1 回答 1

1

更准确一点......以下代码可以工作,但它完全未经测试。如果它不起作用,你应该看看这个线程:如何在 android 中从 assets 文件夹或 raw 文件夹播放视频?

VideoView video = (VideoView) findViewById(R.id.videoView1);
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(video);
video.setMediaController(mediaController);
video.setKeepScreenOn(true);
Uri videoUri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.start);
video.setVideoURI(videoUri);
video.start();

还...

VideoView videoHolder = null;
            videoHolder.setVideoURI(video1);`

崩溃的原因应该是这两行,因为您使用的 videoHolder 必须是null这一点 - 所以你得到一个NullPointerException.

一般来说,您应该将video字段的类型更改为VideoView,因此您不需要转换到VideoView您使用它的任何地方。

于 2013-03-23T22:37:40.023 回答