我一直在尝试创建一个显示 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)