简体   繁体   中英

Logcat error and app keeps crashing with android.view.WindowManager$BadTokenException?

I keep getting this error and I'm not sure what to do anymore, please, can you help?

Logcat logs

  Caused by: android.view.WindowManager$BadTokenException: Unable to add window android.view.ViewRootImpl$W@17b6660 -- permission denied for window type 2002
            at android.view.ViewRootImpl.setView(ViewRootImpl.java:931)
            at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:387)
            at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:95)
            at com.example.littlezero2.Shimeji.ShimejiView (Shimeji.java:182)
            at com.example.littlezero2.Shimeji.pause (Shimeji.java:111)
            at com.example.littlezero2.Shimeji.onCreate (Shimeji.java:95)
            at android.app.ActivityThread.handleCreateService(ActivityThread.java:3953)
            at android.app.ActivityThread.access$1500(ActivityThread.java:219) 
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1875) 
            at android.os.Handler.dispatchMessage(Handler.java:107) 
            at android.os.Looper.loop(Looper.java:214) 
            at android.app.ActivityThread.main(ActivityThread.java:7356) 
            at java.lang.reflect.Method.invoke(Native Method) 
            at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)

Okay, so this is a snippet of where the crash is happening I hope this helps. I'm not getting any errors in the build and I have already run it through the debugger, I've changed the phone part to WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY but still the same error occurs from Logcat. I have traced the error from the three lines given in the logcat.

Line 182  wm.addView ( view, params );
Line 111  ShimejiView ();
Line 95 pause ();

I have an error?

The code where it's coming from.

Java

@Override
        public IBinder onBind(Intent intent) {
            return mBinder;
        }
        private final IBinder mBinder = new LocalBinder();
    
        public class LocalBinder extends Binder {
            Shimeji getService() {
                return Shimeji.this;
            }
        }
    
    
        /** end of binder */
        public void onCreate() {
            super.onCreate();
            Notification();
            pause();
        }
    
    
    
        public void onDestroy(){
            super.onDestroy();
            wm.removeViewImmediate(view);
            nm.cancel(001200);
            if (!isMuted){
                stopsound();
            }
            timing.cancel();
            running.cancel();
        }
    
        protected void pause(){
            if (isPaused) {
                ShimejiView();
                randomsens();
                handler.post(draw());
                sp = MediaPlayer.create(getApplicationContext(), R.raw.start);
                mp = MediaPlayer.create(getApplicationContext(), R.raw.poi);
                bp = MediaPlayer.create(getApplicationContext(), R.raw.baka);
                sp.start();
                isPaused = false;
                view.setOnTouchListener(new action());
    
                builder.setContentText("Poi is running");
                nm.notify(
                        01200,
                        builder.build());
            } else if(!isPaused){
                wm.removeViewImmediate(view);
                if (!isMuted){
                    stopsound();
                }
                timing.cancel();
                running.cancel();
                isPaused = true;
                builder.setContentText("Poi is paused");
                nm.notify(
                        01200,
                        builder.build());
            }
        }
    
    
        private class action implements View.OnTouchListener {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()){
                    case MotionEvent.ACTION_DOWN:
                        idleanimation.stop();
                        view.setBackgroundResource(R.drawable.blink1);
                        blinkanimation = (AnimationDrawable)view.getBackground();
                        blinkanimation.start();
                        return true;
                    case MotionEvent.ACTION_MOVE:
                        int x_cord = (int) event.getRawX();
                        int y_cord = (int) event.getRawY();
                        params.x= (x_cord-(width/2));
                        params.y =(y_cord-(height/2)-50);
                        wm.updateViewLayout(view, params);
                        break;
                    case MotionEvent.ACTION_UP:
                        touchcheck();
                        view.setBackgroundResource(R.drawable.idle1);
                        idleanimation = (AnimationDrawable)view.getBackground();
                        idleanimation.start();
                        break;
                    case MotionEvent.ACTION_OUTSIDE:
                }
                return false;
            }
        }
        private void ShimejiView()
        {
            view = new ImageView(this);
            view.setBackgroundResource(R.drawable.idle1);
            idleanimation = (AnimationDrawable)view.getBackground();
            idleanimation.start();
            view.setOnTouchListener(new action());
            li = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
            wm = (WindowManager) getSystemService(WINDOW_SERVICE);
            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.NO_GRAVITY;
            myview = li.inflate(R.layout.playground, null);
            wm.addView(view,params);
            Display display = wm.getDefaultDisplay();
            Point size = new Point();
            display.getSize(size);
            width = size.x;
            height = size.y;
        }

This is deprecated:

WindowManager.LayoutParams.TYPE_PHONE

You're using it in your ShimejiView method.

"This constant was deprecated in API level 26. for non-system apps. Use TYPE_APPLICATION_OVERLAY instead."

Found in the documentation: See here: https://developer.android.com/reference/android/view/WindowManager.LayoutParams

Maybe that is your issue?

It matches the trace, if I had to put money on it that would be it. You should check out the Window Manager documentation if that's not it and maybe you'll find something else.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM