奇怪的行为 - VideoViewer Fragment无法使用MediaController

时间:2011-06-22 17:52:55

标签: android android-videoview android-fragments

我正在尝试创建一个VideoViewer片段。以下是我尝试的2个案例和观察结果。对于这两种情况,片段的视图从以下布局中膨胀:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" 
android:layout_height="match_parent"
android:gravity="center">

<VideoView android:id="@+id/fragment_video_viewer_video"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_gravity="center" />
</RelativeLayout>

案例1:没有媒体控制器的VideoView - 正常工作。以下是onActivityCreated方法的代码

/* Initialize the variables here, retrieve views and  */
@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    // Assignment for the local variables
    mVideoView = (VideoView) getView().findViewById(R.id.fragment_video_viewer_video);
    mVideoView.setVideoPath("/sdcard/family_guy_test.3gp");//(videoUri);
    mVideoView.requestFocus();
    mVideoView.start();
}

案例2:带媒体控制器的VideoView - 应用程序在触摸VideoView时崩溃。以下是onActivityCreated方法的代码

/* Initialize the variables here, retrieve views and  */
@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    // Assignment for the local variables
    mVideoView = (VideoView) getView().findViewById(R.id.fragment_video_viewer_video);
    mVideoView.setVideoPath("/sdcard/family_guy_test.3gp");//(videoUri);
    MediaController mediaController = new MediaController(getActivity().getApplicationContext());
    mediaController.setAnchorView(mVideoView);
    mVideoView.setMediaController(mediaController);
    mVideoView.requestFocus();
}

logcat输出如下:

WARN/WindowManager(124): Failed looking up window
WARN/WindowManager(124): java.lang.IllegalArgumentException: Requested window null does not exist
WARN/WindowManager(124):     at com.android.server.WindowManagerService.windowForClientLocked(WindowManagerService.java:9398)
WARN/WindowManager(124):     at com.android.server.WindowManagerService.addWindow(WindowManagerService.java:1906)
WARN/WindowManager(124):     at com.android.server.WindowManagerService$Session.add(WindowManagerService.java:6876)
WARN/WindowManager(124):     at android.view.IWindowSession$Stub.onTransact(IWindowSession.java:66)
WARN/WindowManager(124):     at com.android.server.WindowManagerService$Session.onTransact(WindowManagerService.java:6848)
WARN/WindowManager(124):     at android.os.Binder.execTransact(Binder.java:288)
WARN/WindowManager(124):     at dalvik.system.NativeStart.run(Native Method)
WARN/WindowManager(124): Attempted to add window with token that is not a window: null.  Aborting.
WARN/dalvikvm(8144): threadid=1: thread exiting with uncaught exception (group=0x400287f0)
ERROR/AndroidRuntime(8144): FATAL EXCEPTION: main
ERROR/AndroidRuntime(8144): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?
ERROR/AndroidRuntime(8144):     at android.view.ViewRoot.setView(ViewRoot.java:505)
ERROR/AndroidRuntime(8144):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
ERROR/AndroidRuntime(8144):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
ERROR/AndroidRuntime(8144):     at android.widget.MediaController.show(MediaController.java:304)
ERROR/AndroidRuntime(8144):     at android.widget.MediaController.show(MediaController.java:249)
ERROR/AndroidRuntime(8144):     at android.widget.VideoView.toggleMediaControlsVisiblity(VideoView.java:567)
ERROR/AndroidRuntime(8144):     at android.widget.VideoView.onTouchEvent(VideoView.java:518)
ERROR/AndroidRuntime(8144):     at android.view.View.dispatchTouchEvent(View.java:3766)
ERROR/AndroidRuntime(8144):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863)
ERROR/AndroidRuntime(8144):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863)
ERROR/AndroidRuntime(8144):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863)
ERROR/AndroidRuntime(8144):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863)
ERROR/AndroidRuntime(8144):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863)
ERROR/AndroidRuntime(8144):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1671)
ERROR/AndroidRuntime(8144):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
ERROR/AndroidRuntime(8144):     at android.app.Activity.dispatchTouchEvent(Activity.java:2086)
ERROR/AndroidRuntime(8144):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1655)
ERROR/AndroidRuntime(8144):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863)
ERROR/AndroidRuntime(8144):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863)
ERROR/AndroidRuntime(8144):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863)
ERROR/AndroidRuntime(8144):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863)
ERROR/AndroidRuntime(8144):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863)
ERROR/AndroidRuntime(8144):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1671)
ERROR/AndroidRuntime(8144):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
ERROR/AndroidRuntime(8144):     at android.app.Activity.dispatchTouchEvent(Activity.java:2086)
ERROR/AndroidRuntime(8144):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1655)
ERROR/AndroidRuntime(8144):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1785)
ERROR/AndroidRuntime(8144):     at android.os.Handler.dispatchMessage(Handler.java:99)
ERROR/AndroidRuntime(8144):     at android.os.Looper.loop(Looper.java:123)
ERROR/AndroidRuntime(8144):     at android.app.ActivityThread.main(ActivityThread.java:4627)
ERROR/AndroidRuntime(8144):     at java.lang.reflect.Method.invokeNative(Native Method)
ERROR/AndroidRuntime(8144):     at java.lang.reflect.Method.invoke(Method.java:521)
ERROR/AndroidRuntime(8144):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
ERROR/AndroidRuntime(8144):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
ERROR/AndroidRuntime(8144):     at dalvik.system.NativeStart.main(Native Method)
WARN/ActivityManager(124):   Force finishing activity com.comp.ias/.MainActivity
WARN/ActivityManager(124): Activity pause timeout for HistoryRecord{43f755b0 com.comp.ias/.MainActivity}
WARN/WindowManager(124): No window to dispatch pointer action 1

1 个答案:

答案 0 :(得分:22)

我发现问题实际上是在我使用的环境中。我不得不使用Activity上下文而不是应用程序上下文。更具体地说,我必须这样做:

MediaController mediaController = new MediaController(getActivity());

何时使用哪个上下文非常稀疏的文档。有时,可以使用任何上下文。有时,我们需要使用特定的上下文。我认为使用上下文背后的直觉是,“如果一个类的实例绑定到一个活动,使用活动上下文,否则使用应用程序上下文”。