[报错]token null is not valid is your activity running

1.异常日志

我的这个报错,和网上的报错,所依附的 activity 不太一样,之前还延时创建 悬浮窗,但是没用。

01-04 07:04:25.319 30523-30523/com.fadisu.cpurun E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.fadisu.cpurun, PID: 30523
android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?
   at android.view.ViewRootImpl.setView(ViewRootImpl.java:783)
   at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:359)
   at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:93)
   at com.fadisu.cpurun.fragment.CpuStatusFragment.showFloatWindow(CpuStatusFragment.java:260)
   at com.fadisu.cpurun.fragment.CpuStatusFragment.onClick(CpuStatusFragment.java:210)
   at android.view.View.performClick(View.java:6300)
   at android.view.View$PerformClick.run(View.java:24941)
   at android.os.Handler.handleCallback(Handler.java:790)
   at android.os.Handler.dispatchMessage(Handler.java:99)
   at android.os.Looper.loop(Looper.java:164)
   at android.app.ActivityThread.main(ActivityThread.java:6523)
   at java.lang.reflect.Method.invoke(Native Method)
   at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:857)

2.代码片段

mParams.type = WindowManager.LayoutParams.TYPE_TOAST; 这句出问题了

改成

mParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;

就好了,可是想要达到不需要权限申请的悬浮窗啊?,如何两全其美

    private void showFloatWindow() {
        mResultTv = new TextView(mContext);
        mResultTv.setBackgroundColor(mContext.getColor(R.color.float_bg));
        mResultTv.setTextColor(mContext.getColor(R.color.white));
        mResultTv.setOnTouchListener(new View.OnTouchListener() {
            int lastX = 0;
            int lastY = 0;
            int paramX = 0;
            int paramY = 0;

            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                switch (motionEvent.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        lastX = (int) motionEvent.getRawX();
                        lastY = (int) motionEvent.getRawY();
                        paramX = mParams.x;
                        paramY = mParams.y;
                        break;
                    case MotionEvent.ACTION_MOVE:
                        int dx = (int) motionEvent.getRawX() - lastX;
                        int dy = (int) motionEvent.getRawY() - lastY;
                        mParams.x = paramX + dx;
                        mParams.y = paramY + dy;
                        // update float window
                        mWm.updateViewLayout(mResultTv, mParams);
                        break;
                }
                return true;
            }
        });

        mParams = new WindowManager.LayoutParams();
        mParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
        mParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
        // 悬浮窗的核心
        mParams.type = WindowManager.LayoutParams.TYPE_TOAST;
        // 设置浮动窗口不可聚焦(实现操作除浮动窗口外的其他可见窗口的操作)
        mParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
        mParams.format = PixelFormat.TRANSPARENT;

        mWm.addView(mResultTv, mParams);
    }

    private void hideFloatWindow() {
        mWm.removeView(mResultTv);
    }

so,还需要只加权限

    private static final int REQUEST_CODE = 1;

    //判断权限
    private boolean isCanDrawOverlays(Context context) {
        Boolean result = true;
        if (Build.VERSION.SDK_INT >= 23) {
            try {
                Class clazz = Settings.class;
                Method canDrawOverlays = clazz.getDeclaredMethod("canDrawOverlays", Context.class);
                result = (Boolean) canDrawOverlays.invoke(null, context);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return result;
    }

    //申请权限
    private void requestAlertWindowPermission() {
        Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
        intent.setData(Uri.parse("package:" + mContext.getPackageName()));
        startActivityForResult(intent, REQUEST_CODE);
    }

AndroidManifest.xml

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>

你可能感兴趣的:(Android基础)