程序之前是好用的可是添加了个Spinner运行点击Spinner控件时,系统崩溃,出现如下报错:E/AndroidRuntime(26975): FATAL EXCEPTION: mainE/AndroidRuntime(26975): android.view.WindowManager$BadTokenException: Unable to add window -- token android.app.LocalActivityManager$LocalActivityRecord@4137c7b8 is not valid; is your activity running?E/AndroidRuntime(26975): at android.view.ViewRootImpl.setView(ViewRootImpl.java:704)E/AndroidRuntime(26975): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:313)E/AndroidRuntime(26975): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:215)E/AndroidRuntime(26975): at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:140)E/AndroidRuntime(26975): at android.view.Window$LocalWindowManager.addView(Window.java:539)E/AndroidRuntime(26975): at android.app.Dialog.show(Dialog.java:278)E/AndroidRuntime(26975): at android.app.AlertDialog$Builder.show(AlertDialog.java:932)E/AndroidRuntime(26975): at android.widget.Spinner$DialogPopup.show(Spinner.java:703)E/AndroidRuntime(26975): at android.widget.Spinner.performClick(Spinner.java:458)E/AndroidRuntime(26975): at android.view.View$PerformClick.run(View.java:14325)E/AndroidRuntime(26975): at android.os.Handler.handleCallback(Handler.java:605)E/AndroidRuntime(26975): at android.os.Handler.dispatchMessage(Handler.java:92)E/AndroidRuntime(26975): at android.os.Looper.loop(Looper.java:137)E/AndroidRuntime(26975): at android.app.ActivityThread.main(ActivityThread.java:4512)E/AndroidRuntime(26975): at java.lang.reflect.Method.invokeNative(Native Method)E/AndroidRuntime(26975): at java.lang.reflect.Method.invoke(Method.java:511)E/AndroidRuntime(26975): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:978)E/AndroidRuntime(26975): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:745)E/AndroidRuntime(26975): at dalvik.system.NativeStart.main(Native Method)问题原因及解决方法:由报错可知是弹出的Spinner选项窗口找不到可以依附的context,但我使用的明明是getParent()的上下文,当时奇怪了,纠结了好久,搞了半天,后发现如下网站找到了解决方法,原来是加载Acitivity的布局文件方式导致的,但为什么会导致这种情况呢?先看看两种加载方式:之前使用的是:@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);this.setContentView(viewToLoad);//}修改后:@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);View viewToLoad = LayoutInflater.from(this.getParent()). inflate(R.layout.patient_nursing_base, null);//这是重点哦this.setContentView(viewToLoad);// this.setContentView(R.layout.patient_nursing_base);//把原来的注释掉}显然,修改后加载布局文件方式中使用了this.getParent(),获取到了上一级context,这样再弹出子窗口就能找到依附的对象。
android.view.WindowManager$BadTokenException: Unable to add window --Unabletoaddwindow--tokenandroid
最新推荐文章于 2022-04-09 22:49:28 发布