BadTokenException:Unable to add window …… is your activity running?is not valid; is your activity ru

问题发生环境:

TabAcitivity中的每个tab标签管理一个ActivityGroup,每个ActivityGroup管理多个Acitivity,

在其中一个Activity中在使用Spinner控件时,使用代码:

  resolutionSpinner = (Spinner) findViewById(R.id.resolutionSpinner);	    
  resolutionAdapter = ArrayAdapter.createFromResource(this.getParent(), R.array.resolutionItem, android.R.layout.simple_spinner_item);
  resolutionAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  resolutionSpinner.setAdapter(resolutionAdapter);

点击 Spinner控件 时,系统崩溃,出现如下报错:

E/AndroidRuntime(26975): FATAL EXCEPTION: main
E/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的布局文件方式导致的,但为什么会导致这种情况呢?先看看两种加载方式:

之前使用的是:

onCreate(savedInstanceState);  
this.setContentView(R.layout.camera_video1)
 
 

修改后:

super.onCreate(savedInstanceState);
View viewToLoad = LayoutInflater.from(this.getParent()).inflate(R.layout.camera_video1, null);
this.setContentView(viewToLoad);

显然,修改后加载布局文件方式中使用了this.getParent(),获取到了上一级context,这样再弹出子窗口就能找到依附的对象。

参考网站:

http://stackoverflow.com/questions/7609519/android-spinner-error-android-view-windowmanagerbadtokenexception-unable-to

你可能感兴趣的:(BadTokenException:Unable to add window …… is your activity running?is not valid; is your activity ru)