Unable to add window -- token null is not for an application,普通应用程序悬浮窗口(TYPE_APPLICATION)

今天在做悬浮窗口的时候遇到了一个bug。本来是做WindowManager.LayoutParams.type = TYPE_PHONE,在所有应用程序上的悬浮窗口,后面改成只能在本Activity中,于是WindowManager.LayoutParams.type改成TYPE_APPLICATION,然后就遇到了下面的bug。

这里要说的是由于要做所有应用程序上的悬浮窗口,所以WindowManager.LayoutParams一开始是在Application初始化,WindowManager也是用Application的Context获取的。然后改了type之后就遇到了下面这个bug。

E/AndroidRuntime: FATAL EXCEPTION: main
                               Process: com.silas.mybitmapshader, PID: 9281
                               java.lang.RuntimeException: Unable to start activity ComponentInfo{com.silas.mybitmapshader/com.silas.mybitmapshader.MainActivity}: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
                               at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2219)
                               at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2269)
                               at android.app.ActivityThread.access$800(ActivityThread.java:135)
                               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
                               at android.os.Handler.dispatchMessage(Handler.java:102)
                               at android.os.Looper.loop(Looper.java:136)
                               at android.app.ActivityThread.main(ActivityThread.java:5045)
                               at java.lang.reflect.Method.invokeNative(Native Method)
                               at java.lang.reflect.Method.invoke(Method.java:515)
                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
                               at dalvik.system.NativeStart.main(Native Method)
                            Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
                               at android.view.ViewRootImpl.setView(ViewRootImpl.java:543)
                               at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:259)
                               at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
                               at com.silas.mybitmapshader.MainActivity.createView(MainActivity.java:70)
                               at com.silas.mybitmapshader.MainActivity.onCreate(MainActivity.java:31)
                               at android.app.Activity.performCreate(Activity.java:5231)
                               at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
                               at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2163)
                               at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2269) 
                               at android.app.ActivityThread.access$800(ActivityThread.java:135) 
                               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 
                               at android.os.Handler.dispatchMessage(Handler.java:102) 
                               at android.os.Looper.loop(Looper.java:136) 
                               at android.app.ActivityThread.main(ActivityThread.java:5045) 
                               at java.lang.reflect.Method.invokeNative(Native Method) 
                               at java.lang.reflect.Method.invoke(Method.java:515) 
                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 
                               at dalvik.system.NativeStart.main(Native Method) 


第70行是这个代码,是添加窗口的代码,如下图所示:

后面寻思了下 Unable to add window -- token null is not for an application,不能添加窗口,应该是参数的问题了,但是参数看来看去也没有啥问题,那就是context的问题了。

后面把WindowManager的Context换成这个Activity的Context后,bug就解决了。

你可能感兴趣的:(Unable to add window -- token null is not for an application,普通应用程序悬浮窗口(TYPE_APPLICATION))