samuelebe
samuelebe

Reputation: 149

Unable to start activity component for VideoView

I am trying to display a youtube video in full screen mode using VideoView. I have written small piece of code before implementing it but I am getting nullPointerException. Below is my sample code I wrote.

I made entry in AndroidManifest.xml file for permissions..

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

I have created a videoview layout in main.xml

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

My source file looks like this..

public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
VideoView vv = (VideoView) findViewById(R.id.VideoView);                        
MediaController mc=new MediaController(this);
mc.setEnabled(true);
vv.setVideoURI(Uri.parse("http://www.youtube.com/v/Iq81rUGQofk"));
vv.setMediaController(mc);
vv.requestFocus();
vv.showContextMenu();
vv.start(); 
setContentView(R.layout.main);
}

This is throwing, below exception.

11-19 03:11:50.558: E/AndroidRuntime(11488): FATAL EXCEPTION: main
11-19 03:11:50.558: E/AndroidRuntime(11488): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mydemoapp/com.example.mydemoapp.MainActivity}: java.lang.NullPointerException
11-19 03:11:50.558: E/AndroidRuntime(11488):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1999)
11-19 03:11:50.558: E/AndroidRuntime(11488):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2024)
11-19 03:11:50.558: E/AndroidRuntime(11488):    at android.app.ActivityThread.access$600(ActivityThread.java:126)
11-19 03:11:50.558: E/AndroidRuntime(11488):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1159)
11-19 03:11:50.558: E/AndroidRuntime(11488):    at android.os.Handler.dispatchMessage(Handler.java:99)
11-19 03:11:50.558: E/AndroidRuntime(11488):    at android.os.Looper.loop(Looper.java:137)
11-19 03:11:50.558: E/AndroidRuntime(11488):    at android.app.ActivityThread.main(ActivityThread.java:4479)
11-19 03:11:50.558: E/AndroidRuntime(11488):    at java.lang.reflect.Method.invokeNative(Native Method)
11-19 03:11:50.558: E/AndroidRuntime(11488):    at java.lang.reflect.Method.invoke(Method.java:511)
11-19 03:11:50.558: E/AndroidRuntime(11488):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
11-19 03:11:50.558: E/AndroidRuntime(11488):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
11-19 03:11:50.558: E/AndroidRuntime(11488):    at dalvik.system.NativeStart.main(Native  Method)
11-19 03:11:50.558: E/AndroidRuntime(11488): Caused by: java.lang.NullPointerException
11-19 03:11:50.558: E/AndroidRuntime(11488):    at com.example.mydemoapp.MainActivity.onCreate(MainActivity.java:30)
11-19 03:11:50.558: E/AndroidRuntime(11488):    at android.app.Activity.performCreate(Activity.java:4628)
11-19 03:11:50.558: E/AndroidRuntime(11488):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
11-19 03:11:50.558: E/AndroidRuntime(11488):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1963)

Please let me know if my approach to this is not correct.

Upvotes: 0

Views: 1173

Answers (2)

Akshay
Akshay

Reputation: 2534

You are using VideoView vv = (VideoView) findViewById(R.id.VideoView); before calling setcontentView() hence you are getting nullPointer Exception.

use your Videoview after your view has been set.

Upvotes: 2

Praful Bhatnagar
Praful Bhatnagar

Reputation: 7435

move the setContentView(R.layout.main); after the call to super.onCreate(). Like following

public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);

setContentView(R.layout.main);

VideoView vv = (VideoView) findViewById(R.id.VideoView);                        
MediaController mc=new MediaController(this);
mc.setEnabled(true);
vv.setVideoURI(Uri.parse("http://www.youtube.com/v/Iq81rUGQofk"));
vv.setMediaController(mc);
vv.requestFocus();
vv.showContextMenu();
vv.start(); 

}

Upvotes: 0

Related Questions