Android 使用VideoView时应用程序崩溃

Android 使用VideoView时应用程序崩溃,android,Android,我一直在尝试创建一个显示视频视图的应用程序,它下面有三个按钮,如video.xml中所示,这是相对布局的一部分 <Button android:id="@+id/button1" style="?android:attr/buttonStyleSmall" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParen

我一直在尝试创建一个显示视频视图的应用程序,它下面有三个按钮,如
video.xml
中所示,这是
相对布局的一部分

<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" />
这是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)

更准确一点说。。。 下面的代码可以工作,但完全未经测试。如果它不起作用,您应该查看此线程:

也。。。 崩溃的原因应该是这两行,因为您使用的videoHolder在这一点上必须是
null
a,所以您得到了
NullPointerException


一般来说,您应该将
video
字段的类型更改为
VideoView
,这样您就不需要在使用它的任何地方强制转换到
VideoView

发布日志。与普拉格纳尼所说的完全相同。当你说崩溃时,你应该显示崩溃报告,即logcat Trace我怎么看logcat?我的很多朋友告诉我,我应该知道什么是LogCat,但他们永远不会告诉我!因此,请使用ACRA。如果你是在其他手机上测试的。这是谷歌Play上的一个应用程序吗?这就是你如何组合两篇帖子@fishThank Fish它的工作原理。真的帮了我的忙。4月25日,如果您想查看该应用程序,请转到Google Play并键入com.apw.games.rpg.medium.silver,或者,如果您想要预发布,请发送电子邮件至alex364981@gmail.com
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)
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);`