最近我遇到了下面给出的错误。
错误是:
09-30 16:11:37.693: ERROR/AndroidRuntime(699): FATAL EXCEPTION: main
09-30 16:11:37.693: ERROR/AndroidRuntime(699): android.view.WindowManager$BadTokenException: Unable to add window -- token android.app.LocalActivityManager$LocalActivityRecord@407f4de8 is not valid; is your activity running?
09-30 16:11:37.693: ERROR/AndroidRuntime(699): at android.view.ViewRoot.setView(ViewRoot.java:527)
09-30 16:11:37.693: ERROR/AndroidRuntime(699): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
09-30 16:11:37.693: ERROR/AndroidRuntime(699): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
09-30 16:11:37.693: ERROR/AndroidRuntime(699): at android.view.Window$LocalWindowManager.addView(Window.java:424)
09-30 16:11:37.693: ERROR/AndroidRuntime(699): at android.app.Dialog.show(Dialog.java:241)
09-30 16:11:37.693: ERROR/AndroidRuntime(699): at android.app.AlertDialog$Builder.show(AlertDialog.java:802)
09-30 16:11:37.693: ERROR/AndroidRuntime(699): at android.widget.Spinner.performClick(Spinner.java:260)
09-30 16:11:37.693: ERROR/AndroidRuntime(699): at android.view.View$PerformClick.run(View.java:9080)
09-30 16:11:37.693: ERROR/AndroidRuntime(699): at android.os.Handler.handleCallback(Handler.java:587)
09-30 16:11:37.693: ERROR/AndroidRuntime(699): at android.os.Handler.dispatchMessage(Handler.java:92)
09-30 16:11:37.693: ERROR/AndroidRuntime(699): at android.os.Looper.loop(Looper.java:123)
09-30 16:11:37.693: ERROR/AndroidRuntime(699): at android.app.ActivityThread.main(ActivityThread.java:3683)
09-30 16:11:37.693: ERROR/AndroidRuntime(699): at java.lang.reflect.Method.invokeNative(Native Method)
09-30 16:11:37.693: ERROR/AndroidRuntime(699): at java.lang.reflect.Method.invoke(Method.java:507)
09-30 16:11:37.693: ERROR/AndroidRuntime(699): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
09-30 16:11:37.693: ERROR/AndroidRuntime(699): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
09-30 16:11:37.693: ERROR/AndroidRuntime(699): at dalvik.system.NativeStart.mai
我想分享一下我是如何解决它的。
答案 0 :(得分:1)
我找到了badTokenExcaption的解决方案
在你的活动的onCreate()方法中,用
替换行setContentView(R.layout.XXXXX)View viewToLoad = LayoutInflater.from(this.getParent()).inflate(R.layout.XXXXX, null);
this.setContentView(viewToLoad);
并通过以下行替换微调器代码
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource( this, R.array.medicine_types, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
spDosageType.setAdapter(adapter);