app showing error unable to add window -- token null is not valid; is your activity running?

2k views Asked by At

I want to show a video on popup window from service but when I start the service my application crashes show this error : -

android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?

My code of video player service is here :

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) {

        }
    });

I'm calling this service from myactivity on resume method.

Error stack of this error is here :

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)

Why I'm getting this error and how to solve this.

1

There are 1 answers

2
AudioBubble On

"WindowManager$BadTokenException" this error occur when you using window level object like dialog and before dismiss your activity finished then this error occur. so you should put condition that activity is destroyed or not then please use context.