播放视频时应用程序崩溃

时间:2014-11-07 14:15:06

标签: android exception android-videoview

我正在以全屏模式播放视频。我使用videoview播放了一段视频,播放效果不错但不幸的是,在视频处于播放状态时点按视频会崩溃。播放后视频,如果我们点击后退按钮应用程序已崩溃。以上两个条件我得到相同的异常android.view.WindowManager$BadTokenException。这是我的代码,如下所示。

public void previewVideo(Uri mediaUri) {         

            videoPreview.setVideoURI(mediaUri);
            MediaController mediaController = new MediaController(getApplicationContext());
            mediaController.setAnchorView(videoPreview);
            videoPreview.setMediaController(mediaController);
            videoPreview.requestFocus();
            videoPreview.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                // Close the progress bar and play the video
                public void onPrepared(MediaPlayer mp) {

                    videoPreview.start();
                }
            });

异常

11-07 19:22:26.524    1229-1229/com.ontometrics.parent E/InputEventReceiver﹕ Exception dispatching input event.
11-07 19:22:26.524    1229-1229/com.ontometrics.parent E/MessageQueue-JNI﹕ Exception in MessageQueue callback: handleReceiveCallback
11-07 19:22:26.534    1229-1229/com.ontometrics.parent E/MessageQueue-JNI﹕ android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?
            at android.view.ViewRootImpl.setView(ViewRootImpl.java:771)
            at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:278)
            at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
            at android.widget.MediaController.show(MediaController.java:346)
            at android.widget.MediaController.show(MediaController.java:306)
            at 
            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2434)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2123)

1 个答案:

答案 0 :(得分:4)

使用

MediaController mediaController = new MediaController(MyActivity.this);

而不是

MediaController mediaController = new MediaController(getApplicationContext());