【问题标题】:Logcat error and app keeps crashing with android.view.WindowManager$BadTokenException?Logcat 错误和应用程序不断因 android.view.WindowManager$BadTokenException 而崩溃?
【发布时间】:2020-12-25 09:37:21
【问题描述】:

我不断收到此错误,我不知道该怎么办了,请问您能帮忙吗?

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)

好的,所以这是崩溃发生位置的 sn-p 我希望这是 有帮助。我在构建中没有收到任何错误,我已经运行了 通过调试器,我将电话部分更改为 WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY 但 Logcat 仍然出现相同的错误。我已经从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;
        }

【问题讨论】:

    标签: javascript java android sdk


    【解决方案1】:

    已弃用:

    WindowManager.LayoutParams.TYPE_PHONE
    

    您在 ShimejiView 方法中使用它。

    “此常量在 API 级别 26 中已被弃用。对于非系统应用。请改用 TYPE_APPLICATION_OVERLAY。”

    在文档中找到: 看这里: https://developer.android.com/reference/android/view/WindowManager.LayoutParams

    也许这是你的问题?

    它与痕迹相匹配,如果我必须在上面投入资金,那就是它。如果不是这样,您应该查看窗口管理器文档,也许您会找到其他内容。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多