我需要在我的应用程序中实现 TabHost 中的嵌套活动。我使用这样的 ActivityGroup 类:
public class CustomActivityGroup extends ActivityGroup {
View rootView;
public static CustomActivityGroup group;
private ArrayList<View> history;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.history = new ArrayList<View>(); group = this;
View view = getLocalActivityManager().startActivity("LoginActivity", new Intent(this,RegisterActivity.class) .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
.getDecorView();
replaceView(view);
}
@Override
protected void onResume() {
super.onResume();
this.history = new ArrayList<View>();
group = this;
}
public void replaceView(View v) {
if (history.size() == 0) {
if (rootView != null) {
history.add(rootView);
rootView = null;
}
}
history.add(v);
setContentView(v);
}
public void back() {
try {
if (history.size() > 0) {
history.remove(history.size() - 1);
if (history.size() > 0) {
setContentView(history.get(history.size() - 1));
} else {
finish();
}
} else {
finish();
}
} catch (Exception ex) {
}
}
public int getHistorySize() {
return history.size();
}
@Override
public void onBackPressed() {
try {
CustomActivityGroup.group.back();
} catch (Exception ex) {
}
return;
}
}
在我的 TabActivity 中:
intent = new Intent().setClass(this, CustomActivityGroup.class);
spec = tabHost.newTabSpec("tab_2").setIndicator("Sign up",getResources().getDrawable(R.drawable.signup)).setContent(intent);
tabHost.addTab(spec);
但是当我点击相应的标签时,我得到
FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.assignmentexpert/com.activitygroups.CustomActivityGroup}: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.assignmentexpert/com.assignmentexpert.RegisterActivity}: android.view.WindowManager$BadTokenException: Unable to add window -- token android.app.LocalActivityManager$LocalActivityRecord@405ceb00 is not valid; is your activity running?
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
at android.app.ActivityThread.startActivityNow(ActivityThread.java:1491)
at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127)
at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:676)
at android.widget.TabHost.setCurrentTab(TabHost.java:348)
at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:141)
at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:456)
at android.view.View.performClick(View.java:2485)
at android.view.View$PerformClick.run(View.java:9080)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:3687)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.assignmentexpert/com.assignmentexpert.RegisterActivity}: android.view.WindowManager$BadTokenException: Unable to add window -- token android.app.LocalActivityManager$LocalActivityRecord@405ceb00 is not valid; is your activity running?
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
at android.app.ActivityThread.startActivityNow(ActivityThread.java:1491)
at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127)
at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
at com.activitygroups.CustomActivityGroup.onCreate(CustomActivityGroup.java:31)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
... 18 more
Caused by: Unable to add window -- token android.app.LocalActivityManager$LocalActivityRecord@405ceb00 is not valid; is your activity running?
at android.view.ViewRoot.setView(ViewRoot.java:532)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
at android.view.Window$LocalWindowManager.addView(Window.java:424)
at android.app.Dialog.show(Dialog.java:241)
at com.asynctaskbase.AbstractTaskProgressDialogFragment.onCreateDialog(AbstractTaskProgressDialogFragment.java:68)
at com.asynctaskbase.AbstractTaskProgressDialogFragment.onCreateDialog(AbstractTaskProgressDialogFragment.java:1)
at android.support.v4.app.DialogFragment.getLayoutInflater(DialogFragment.java:295)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:871)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1083)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:635)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1431)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:523)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1129)
at android.app.Activity.performStart(Activity.java:3791)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1624)
... 24 more
我做错了什么?