Reputation: 7
I have been trying to create an app that shows a VideoView and under it there are three buttons, as shown here in video.xml
, which is part of a 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" />
Then, I have the following source code in VideoActivity.java
, which is set under the onCreate
Method.
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();
Here is the LogCat Report
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)
Upvotes: 0
Views: 3042
Reputation: 828
To be a little bit more precise... The following code could work but it's totally untested. If it does not work you should take a look into this thread: How to play videos in android from assets folder or raw folder?
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);`
Reason for your crash should be these two lines, because you are using videoHolder which must be null
a this point - so you get a NullPointerException
.
In general you should change the type of the video
field to VideoView
, so you do not need to cast to VideoView
anywhere you use it.
Upvotes: 1