1

我开发了一个应用程序,其中有 4 个选项卡 A、B、C、D。每个选项卡都包含一个活动。在第四个选项卡中,DI 添加了一个 ActivityGroup,其中我还有 3 个活动 X、Y、Z。在我添加活动组的第四个选项卡中,我收到 StackOverflow 错误。请帮助我解决此错误。

这是我的堆栈跟踪:--

06-30 18:22:26.521: ERROR/AndroidRuntime(1293): android.view.WindowManager$BadTokenException: Unable to add window -- token android.app.LocalActivityManager$LocalActivityRecord@62668448 is not valid; is your activity running?
06-30 18:22:26.521: ERROR/AndroidRuntime(1293):     at android.view.ViewRoot.setView(ViewRoot.java:468)
06-30 18:22:26.521: ERROR/AndroidRuntime(1293):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
06-30 18:22:26.521: ERROR/AndroidRuntime(1293):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
06-30 18:22:26.521: ERROR/AndroidRuntime(1293):     at android.view.Window$LocalWindowManager.addView(Window.java:424)
06-30 18:22:26.521: ERROR/AndroidRuntime(1293):     at android.app.Dialog.show(Dialog.java:239)
06-30 18:22:26.521: ERROR/AndroidRuntime(1293):     at android.app.AlertDialog$Builder.show(AlertDialog.java:802)
06-30 18:22:26.521: ERROR/AndroidRuntime(1293):     at android.widget.Spinner.performClick(Spinner.java:257)
06-30 18:22:26.521: ERROR/AndroidRuntime(1293):     at android.view.View.onTouchEvent(View.java:4179)
06-30 18:22:26.521: ERROR/AndroidRuntime(1293):     at android.view.View.dispatchTouchEvent(View.java:3709)
06-30 18:22:26.521: ERROR/AndroidRuntime(1293):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:852)
06-30 18:22:26.521: ERROR/AndroidRuntime(1293):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
06-30 18:22:26.521: ERROR/AndroidRuntime(1293):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
06-30 18:22:26.521: ERROR/AndroidRuntime(1293):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
06-30 18:22:26.521: ERROR/AndroidRuntime(1293):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
06-30 18:22:26.521: ERROR/AndroidRuntime(1293):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
06-30 18:22:26.521: ERROR/AndroidRuntime(1293):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
06-30 18:22:26.521: ERROR/AndroidRuntime(1293):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
06-30 18:22:26.521: ERROR/AndroidRuntime(1293):     at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
06-30 18:22:26.521: ERROR/AndroidRuntime(1293):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
06-30 18:22:26.521: ERROR/AndroidRuntime(1293):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
06-30 18:22:26.521: ERROR/AndroidRuntime(1293):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
06-30 18:22:26.521: ERROR/AndroidRuntime(1293):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
06-30 18:22:26.521: ERROR/AndroidRuntime(1293):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
06-30 18:22:26.521: ERROR/AndroidRuntime(1293):     at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
06-30 18:22:26.521: ERROR/AndroidRuntime(1293):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
06-30 18:22:26.521: ERROR/AndroidRuntime(1293):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
06-30 18:22:26.521: ERROR/AndroidRuntime(1293):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
06-30 18:22:26.521: ERROR/AndroidRuntime(1293):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
06-30 18:22:26.521: ERROR/AndroidRuntime(1293):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
06-30 18:22:26.521: ERROR/AndroidRuntime(1293):     at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
06-30 18:22:26.521: ERROR/AndroidRuntime(1293):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
06-30 18:22:26.521: ERROR/AndroidRuntime(1293):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
06-30 18:22:26.521: ERROR/AndroidRuntime(1293):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
06-30 18:22:26.521: ERROR/AndroidRuntime(1293):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
06-30 18:22:26.521: ERROR/AndroidRuntime(1293):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
06-30 18:22:26.521: ERROR/AndroidRuntime(1293):     at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
06-30 18:22:26.521: ERROR/AndroidRuntime(1293):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
06-30 18:22:26.521: ERROR/AndroidRuntime(1293):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
06-30 18:22:26.521: ERROR/AndroidRuntime(1293):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
06-30 18:22:26.521: ERROR/AndroidRuntime(1293):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
06-30 18:22:26.521: ERROR/AndroidRuntime(1293):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
06-30 18:22:26.521: ERROR/AndroidRuntime(1293):     at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
06-30 18:22:26.521: ERROR/AndroidRuntime(1293):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
06-30 18:22:26.521: ERROR/AndroidRuntime(1293):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
06-30 18:22:26.521: ERROR/AndroidRuntime(1293):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
06-30 18:22:26.521: ERROR/AndroidRuntime(1293):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
06-30 18:22:26.521: ERROR/AndroidRuntime(1293):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
06-30 18:22:26.521: ERROR/AndroidRuntime(1293):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
06-30 18:22:26.521: ERROR/AndroidRuntime(1293):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
06-30 18:22:26.521: ERROR/AndroidRuntime(1293):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
06-30 18:22:26.521: ERROR/AndroidRuntime(1293):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow

这就是我构建标签的方式-------

Resources res = getResources();   
TabHost tabHost = getTabHost();  // The activity TabHost
TabHost.TabSpec spec;  // Reusable TabSpec for each tabO
Intent intent;  // Reusable Intent for each tab

// Create an Intent to launch an Activity for the tab (to be reused)

intent = new Intent().setClass(this,FirstActivity.class);
spec = tabHost.newTabSpec("tab_First");
spec.setIndicator("First",res.getDrawable(R.drawable.ic_tab));
spec.setContent(intent);      
tabHost.addTab(spec);

// 2nd tab
intent = new Intent().setClass(this,SecondActivity.class);
spec = tabHost.newTabSpec("tab_Second");
spec.setIndicator("Second",res.getDrawable(R.drawable.ic_tab));
spec.setContent(intent);
tabHost.addTab(spec);

//3rd Tab
intent = new Intent().setClass(this,ThirdActivity.class);
spec = tabHost.newTabSpec("tab_Third");
spec.setIndicator("Third",res.getDrawable(R.drawable.ic_tab));
spec.setContent(intent);
tabHost.addTab(spec);        

// 4th tab    
intent = new Intent().setClass(this,FourthActivity.class);
spec = tabHost.newTabSpec("tab_Fourth");
spec.setIndicator("Fourth",res.getDrawable(R.drawable.ic_create));
spec.setContent(intent);
tabHost.addTab(spec);           

tabHost.setCurrentTab(0);     
4

4 回答 4

1

我没有看到任何Stackoverflow 错误(你确定你知道 Stackoverflow 是什么吗?)......你的问题是你试图在一个已经完成或不在前台的活动中显示一个对话框。至少,这是我们可以从您的 logcat 跟踪中推断出来的。

于 2011-06-30T12:57:47.383 回答
1

正如@Cristian 所提到的,我也可以说您正在尝试在活动中显示对话框或引用当前活动的上下文,但是每当您应该在使用带有选项卡的 ActivityGroup 时使用上下文时,您需要getParent()使用获取父视图的上下文。

显示对话框时,您需要父活动的上下文:

  // define at Global level and use it inside the activity whenever you want to refer                
  //activity context;
  private Activity activity;  

  //Write this inside onCreate() method.        
  activity = getParent();

现在,每当您应该引用上下文时,请使用此活动上下文值。

于 2011-06-30T13:08:54.377 回答
0

这是一个常见错误,可以在对话框中使用“getApplicationContext”而不是“this”来修复。另请参阅FATAL EXCEPTION: main。当您(从对话框中)尝试使用启动对话框的活动的上下文时,当该活动不再位于前台时,就会发生错误。

于 2011-06-30T13:09:22.210 回答
-1

您创建选项卡的位置创建一个静态上下文并使用您想要创建对话框的活动中的任何位置

于 2012-03-21T13:55:43.100 回答