1.log
020-05-07 16:19:39.989 7144-7144/com.cloudy.jun E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.cloudy.jun, PID: 7144
android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?
at android.view.ViewRootImpl.setView(ViewRootImpl.java:1000)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:428)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:118)
at android.widget.PopupWindow.invokePopup(PopupWindow.java:1595)
at android.widget.PopupWindow.showAtLocation(PopupWindow.java:1353)
at android.widget.PopupWindow.showAtLocation(PopupWindow.java:1319)
at com.cloudy.jun.module.aipark.view.PopupWindowGroup.initPopWindow(PopupWindowGroup.java:159)
at com.cloudy.jun.module.aipark.view.PopupWindowGroup.showMainPopup(PopupWindowGroup.java:59)
at com.cloudy.jun.module.aipark.view.MainIntelligentParkActivity.onWindowFocusChanged(MainIntelligentParkActivity.java:476)
at androidx.appcompat.view.WindowCallbackWrapper.onWindowFocusChanged(WindowCallbackWrapper.java:124)
at com.android.internal.policy.DecorView.onWindowFocusChanged(DecorView.java:1881)
at android.view.View.dispatchWindowFocusChanged(View.java:13780)
at android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java:1487)
at android.view.ViewRootImpl.handleWindowFocusChanged(ViewRootImpl.java:3069)
at android.view.ViewRootImpl.access$1100(ViewRootImpl.java:178)
at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:4796)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:213)
at android.app.ActivityThread.main(ActivityThread.java:8178)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1101)
2020-05-07 16:19:40.005 7144-7144/com.cloudy.jun E/[RC:L-crash_main_trb-F]: {"ptid":"7144-1*","stack":"android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?\\n\tat android.view.ViewRootImpl.setView(ViewRootImpl.java:1000)\\n\tat android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:428)\\n\tat android.view.WindowManagerImpl.addView(WindowManagerImpl.java:118)\\n\tat android.widget.PopupWindow.invokePopup(PopupWindow.java:1595)\\n\tat android.widget.PopupWindow.showAtLocation(PopupWindow.java:1353)\\n\tat android.widget.PopupWindow.showAtLocation(PopupWindow.java:1319)\\n\tat com.cloudy.jun.module.aipark.view.PopupWindowGroup.initPopWindow(PopupWindowGroup.java:159)\\n\tat com.cloudy.jun.module.aipark.view.PopupWindowGroup.showMainPopup(PopupWindowGroup.java:59)\\n\tat com.cloudy.jun.module.aipark.view.MainIntelligentParkActivity.onWindowFocusChanged(MainIntelligentParkActivity.java:476)\\n\tat androidx.appcompat.view.WindowCallbackWrapper.onWindowFocusChanged(WindowCallbackWrapper.java:124)\\n\tat com.android.internal.policy.DecorView.onWindowFocusChanged(DecorView.java:1881)\\n\tat android.view.View.dispatchWindowFocusChanged(View.java:13780)\\n\tat android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java:1487)\\n\tat android.view.ViewRootImpl.handleWindowFocusChanged(ViewRootImpl.java:3069)\\n\tat android.view.ViewRootImpl.access$1100(ViewRootImpl.java:178)\\n\tat android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:4796)\\n\tat android.os.Handler.dispatchMessage(Handler.java:107)\\n\tat android.os.Looper.loop(Looper.java:213)\\n\tat android.app.ActivityThread.main(ActivityThread.java:8178)\\n\tat java.lang.reflect.Method.invoke(Native Method)\\n\tat com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513)\\n\tat com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1101)\\n","reason":"android.view.WindowManager$BadTokenException","env":"{\"_nonfatal\":\"true\",\"_device\":\"CLT-AL00\",\"_os\":\"Android\",\"_os_version\":\"10\",\"_resolution\":\"1080x2159\",\"_app_version\":\"2.2.9.5\",\"_manufacture\":\"HUAWEI\",\"_cpu\":\"arm64-v8a\",\"_opengl\":\"3\",\"_ram_current\":\"3352\",\"_ram_total\":\"5666\",\"_disk_current\":\"2162\",\"_disk_total\":\"2162\",\"_bat\":\"100.0\",\"_run\":\"90\",\"_orientation\":\"Portrait\",\"_root\":\"false\",\"_online\":\"true\",\"_muted\":\"true\",\"_background\":\"false\"}"}
2.BUG分析
(1)原因推测
第一次调用showPopupWindow正常,第二次出现bug,可能是内存泄露,第二次仍然使用第一次的context,导致找不到view.
(2)定位
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
Log.d(TAG, "onWindowFocusChanged");
if (hasFocus && !isMainPopupShowing) {//Activity加载完毕
// popupHandler.sendEmptyMessageDelayed(0, 100);
Log.d(TAG, "onWindowFocusChanged hasFocus ");
Log.d(TAG, "context=" + MainIntelligentParkActivity.this);
//在这里崩的
PopupWindowGroup.getInstance(MainIntelligentParkActivity.this).showMainPopup();
isMainPopupShowing = true;
}
}
public static PopupWindowGroup getInstance(Context context){
if (popupWindowGroup==null){
popupWindowGroup=new PopupWindowGroup(context);
}
return popupWindowGroup;
}
【poupWindowGroup】对象写成static,只要程序没有销毁,第二次进入还是还是使用上次popupWindow对象,导致context没有更新,而上次的活动已经销毁,执行到showMainPopup()时找不到Activity。
(3)解决方案
不需要把【poupWindowGroup】写成单例模式,什么时候需要就new一个新的,保证每次获取到的popupWindowGroup对象时,传入的context都是当前Activity的context。