我正在使用ActivityGroup从Activity1中打开Activity 2,它位于TabHost下。所以我有一组Tabs,Activity 1是其中的一部分,但是当我单击Activity 1中的一个按钮时,我希望Activity 2打开但仍然在选项卡下。使用以下代码可以正常工作:
public void replaceContentView(String id, Intent newIntent) {
View view = getLocalActivityManager().startActivity(id,newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)).getDecorView();
this.setContentView(view);
}
然而,在活动2中,我有一些微调,它们会扩展自定义窗口,并要求使用SimpleCursorAdapter:
@Override
public View newDropDownView(Context context, Cursor cursor, ViewGroup parent) {
super.newDropDownView(context, cursor, parent);
View view = View.inflate(context, R.layout.custom_spinner_row, null);
int labColumn = cursor.getColumnIndex("lab");
String getLab = cursor.getString(labColumn);
TextView lab = (TextView)view.findViewById(R.id.CustomSpinnerTitle);
lab.setText(getLab);
return view;
}
我收到此错误:
09-21 02:33:16.644: ERROR/AndroidRuntime(1453): FATAL EXCEPTION: main
09-21 02:33:16.644: ERROR/AndroidRuntime(1453): android.view.WindowManager$BadTokenException: Unable to add window -- token android.app.LocalActivityManager$LocalActivityRecord@40588a90 is not valid; is your activity running?
09-21 02:33:16.644: ERROR/AndroidRuntime(1453): at android.view.ViewRoot.setView(ViewRoot.java:527)
09-21 02:33:16.644: ERROR/AndroidRuntime(1453): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
09-21 02:33:16.644: ERROR/AndroidRuntime(1453): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
09-21 02:33:16.644: ERROR/AndroidRuntime(1453): at android.view.Window$LocalWindowManager.addView(Window.java:424)
09-21 02:33:16.644: ERROR/AndroidRuntime(1453): at android.app.Dialog.show(Dialog.java:241)
09-21 02:33:16.644: ERROR/AndroidRuntime(1453): at android.app.AlertDialog$Builder.show(AlertDialog.java:802)
09-21 02:33:16.644: ERROR/AndroidRuntime(1453): at android.widget.Spinner.performClick(Spinner.java:260)
09-21 02:33:16.644: ERROR/AndroidRuntime(1453): at android.view.View$PerformClick.run(View.java:9080)
09-21 02:33:16.644: ERROR/AndroidRuntime(1453): at android.os.Handler.handleCallback(Handler.java:587)
09-21 02:33:16.644: ERROR/AndroidRuntime(1453): at android.os.Handler.dispatchMessage(Handler.java:92)
09-21 02:33:16.644: ERROR/AndroidRuntime(1453): at android.os.Looper.loop(Looper.java:123)
09-21 02:33:16.644: ERROR/AndroidRuntime(1453): at android.app.ActivityThread.main(ActivityThread.java:3683)
09-21 02:33:16.644: ERROR/AndroidRuntime(1453): at java.lang.reflect.Method.invokeNative(Native Method)
09-21 02:33:16.644: ERROR/AndroidRuntime(1453): at java.lang.reflect.Method.invoke(Method.java:507)
09-21 02:33:16.644: ERROR/AndroidRuntime(1453): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
09-21 02:33:16.644: ERROR/AndroidRuntime(1453): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
09-21 02:33:16.644: ERROR/AndroidRuntime(1453): at dalvik.system.NativeStart.main(Native Method)
我收到此错误的原因是因为活动2实际上没有运行,因为我将活动1的contentView替换为2,以便在选项卡下显示它。但我不知道有任何其他方法可以做到这一点。有没有办法在我的选项卡下运行Activity 2,方法实际上是运行活动而不是仅仅替换1的contentView?
答案 0 :(得分:1)
您正在使用处理View
的ActivityGroup,因此当您使用ActivityGroup时,您无法直接setContentView(R.layout.your_xml);
。在这种情况下,您必须将XML扩展到View。
因此,请将<{1}}替换为
setContentView(R.layout.your_xml)
希望这适合你。