我想說明從服務上的彈出式窗口的視頻,但是當我啓動服務我的應用程序崩潰顯示此錯誤: -顯示錯誤的應用程序無法添加窗口 - 標記null無效;你的活動正在運行?
android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?
我的視頻播放器服務代碼如下:
VideoExtractor VideoExtractor = VideoExtractor.create();
VideoExtractor.extract("FTD9aA4S5BY").enqueue(new Callback<VideoExtractionResult>() {
@Override
public void onResponse(Call<YouTubeExtractionResult> call, Response<VideoExtractionResult> response) {
Log.d(TAG, response.body().getVideoUri().toString());
final VideoView mVideoview = (VideoView)mView.findViewById(R.id.youtubePlayer);
mVideoview.setMediaController(new MediaController(PlayerService.this))
mVideoview.setVideoURI(response.body().getVideoUri());
mVideoview.start();
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
params.gravity = Gravity.TOP | Gravity.LEFT;
params.x = 0;
params.y = 100;
mWindowManager.addView(mView, params);
}
@Override
public void onFailure(Call<YouTubeExtractionResult> call, Throwable t) {
}
});
我從恢復方法中的myactivity調用此服務。這個錯誤的
錯誤堆棧是在這裏:
android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?
at android.view.ViewRootImpl.setView(ViewRootImpl.java:574)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:310)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:86)
at android.widget.MediaController.show(MediaController.java:379)
at android.widget.MediaController.show(MediaController.java:329)
at android.widget.VideoView$2.onPrepared(VideoView.java:454)
at android.media.MediaPlayer$EventHandler.handleMessage(MediaPlayer.java:2815)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5539)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
爲什麼我得到這個錯誤,以及如何解決這個問題。
嘗試答案在這裏http://stackoverflow.com/questions/18662239/android-view-windowmanagerbadtokenexception-unable-to-add-window-on-buider-s/27737641#27737641 – Raghavendra
發表您的活動代碼 –
我的錯誤同時添加mediacontroller到視頻視圖,當我刪除這個eroor沒有發生。 – sasuke