0

我正在尝试使用 VideoView 以全屏模式显示 youtube 视频。我在实现之前编写了一小段代码,但我得到了nullPointerException. 下面是我写的示例代码。

我在 AndroidManifest.xml 文件中输入了权限..

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

我在 main.xml 中创建了一个 videoview 布局

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

我的源文件看起来像这样..

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);
}

这是抛出异常。

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)

如果我的处理方法不正确,请告诉我。

4

2 回答 2

2

VideoView vv = (VideoView) findViewById(R.id.VideoView);在打电话之前使用,setcontentView()因此你得到nullPointer Exception.

Videoview设置视图后使用您的。

于 2012-11-19T11:20:37.057 回答
0

setContentView(R.layout.main);在调用 super.onCreate() 之后移动。喜欢以下

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(); 

}
于 2012-11-19T11:19:12.487 回答