logcat错误,应用程序在android.view.windowmanager$badtokenexception中不断崩溃?

jaql4c8m  于 2021-07-04  发布在  Java
关注(0)|答案(1)|浏览(294)

我一直在犯这个错误,我不知道该怎么办了,拜托,你能帮忙吗?
logcat日志

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)

好的,这是车祸发生的地方的一个片段,希望能有所帮助。我没有在构建中得到任何错误,并且我已经通过调试器运行了它,我已经将电话部分更改为windowmanager.layoutparams.type\u application\u overlay,但是仍然从logcat中发生相同的错误。我已经从日志中给出的三行中找出了错误。

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

我出错了?
密码是从哪里来的。
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;
        }
fslejnso

fslejnso1#

这是不赞成的:

WindowManager.LayoutParams.TYPE_PHONE

你在shimejiview方法中使用它。
“此常量在api级别26中已被弃用。对于非系统应用程序。请改用类型\应用程序\覆盖。“
可在文档中找到:请参见此处:https://developer.android.com/reference/android/view/windowmanager.layoutparams
也许这是你的问题?
它和痕迹吻合,如果我必须在上面放钱的话,那就是了。你应该检查一下窗口管理器文档,如果不是它,也许你会发现一些其他的东西。

相关问题