Bobby Chandra
Bobby Chandra

Reputation: 89

Videoview android crash

I'm currently developing android app using eclipse. But, the app crash when after I put videoView. I put onCompletionListener so if the video end it automaticaly jump to main activity. Here it is the my videoview code:

@Override
     public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.view_video);

     playVideo();
}

public void playVideo(){
         VideoView myVideoView = (VideoView)findViewById(R.id.myvideoview);
         setContentView(myVideoView);

         Uri video = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.endingvid);
         myVideoView.setVideoURI(video);
         myVideoView.setOnCompletionListener(new OnCompletionListener() {
           public void onCompletion(MediaPlayer mp) {
            jumpMain();
           }

          });
         myVideoView.start();
}

And here it is the xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:layout_gravity="center_vertical|center"
   android:background="@drawable/black"
   >

    <VideoView
   android:id="@+id/myvideoview"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content" />
</LinearLayout>

Anyone know what is wrong with my code?

Upvotes: 0

Views: 550

Answers (1)

Blackbelt
Blackbelt

Reputation: 157437

 VideoView myVideoView = (VideoView)findViewById(R.id.myvideoview);
 setContentView(myVideoView);

You can not reference R.id.myvideoview before you set your Layout with setContentView

setContentView(R.layour.layout_with_videoview); 
VideoView myVideoView = (VideoView)findViewById(R.id.myvideoview);

Upvotes: 1

Related Questions