代码如下:
Resources res = getResources(); // Resource object to get Drawables
TabHost tabHost = getTabHost(); // The activity TabHost
TabHost.TabSpec spec; // Resusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
LinearLayout ll = (LinearLayout)tabHost.getChildAt(0);
tw = (TabWidget)ll.getChildAt(1);
RelativeLayout tabIndicator1 = (RelativeLayout) LayoutInflater.from(this).inflate(R.layout.tab_indicator,tw,false);
title = (TextView)tabIndicator1.getChildAt(1);
icon = (ImageView)tabIndicator1.getChildAt(0);
intent = new Intent().setClass(this, HomeActivity.class);
title.setText("Index");
icon.setImageResource(R.drawable.info);
spec = tabHost.newTabSpec("Index").setIndicator(tabIndicator1).setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, TestActivity.class);
title.setText("Test");
icon.setImageResource(R.drawable.test);
spec = tabHost.newTabSpec("Test").setIndicator(tabIndicator1).setContent(intent);
tabHost.addTab(spec); // 这一行报错
项目出现异常如下:
02-22 16:45:02.737: E/AndroidRuntime(6064): java.lang.RuntimeException: Unable to start activity ComponentInfo{XXXXXXX.MyTabActivity}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
02-22 16:45:02.737: E/AndroidRuntime(6064): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1655)
02-22 16:45:02.737: E/AndroidRuntime(6064): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1671)
02-22 16:45:02.737: E/AndroidRuntime(6064): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-22 16:45:02.737: E/AndroidRuntime(6064): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
02-22 16:45:02.737: E/AndroidRuntime(6064): at android.os.Handler.dispatchMessage(Handler.java:99)
02-22 16:45:02.737: E/AndroidRuntime(6064): at android.os.Looper.loop(Looper.java:130)
02-22 16:45:02.737: E/AndroidRuntime(6064): at android.app.ActivityThread.main(ActivityThread.java:3694)
02-22 16:45:02.737: E/AndroidRuntime(6064): at java.lang.reflect.Method.invokeNative(Native Method)
02-22 16:45:02.737: E/AndroidRuntime(6064): at java.lang.reflect.Method.invoke(Method.java:507)
02-22 16:45:02.737: E/AndroidRuntime(6064): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
02-22 16:45:02.737: E/AndroidRuntime(6064): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
02-22 16:45:02.737: E/AndroidRuntime(6064): at dalvik.system.NativeStart.main(Native Method)
02-22 16:45:02.737: E/AndroidRuntime(6064): Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
02-22 16:45:02.737: E/AndroidRuntime(6064): at android.view.ViewGroup.addViewInner(ViewGroup.java:1998)
02-22 16:45:02.737: E/AndroidRuntime(6064): at android.view.ViewGroup.addView(ViewGroup.java:1893)
02-22 16:45:02.737: E/AndroidRuntime(6064): at android.view.ViewGroup.addView(ViewGroup.java:1850)
02-22 16:45:02.737: E/AndroidRuntime(6064): at android.view.ViewGroup.addView(ViewGroup.java:1830)
02-22 16:45:02.737: E/AndroidRuntime(6064): at android.widget.TabWidget.addView(TabWidget.java:418)
02-22 16:45:02.737: E/AndroidRuntime(6064): at android.widget.TabHost.addTab(TabHost.java:252)
02-22 16:45:02.737: E/AndroidRuntime(6064): at com.xxxx.xxxx.activity.MyTabActivity.onCreate(MyTabActivity.java:85)
02-22 16:45:02.737: E/AndroidRuntime(6064): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-22 16:45:02.737: E/AndroidRuntime(6064): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1619)
02-22 16:45:02.737: E/AndroidRuntime(6064): ... 11 more
解决方法: 重新创建一个新的View
Resources res = getResources(); // Resource object to get Drawables
TabHost tabHost = getTabHost(); // The activity TabHost
TabHost.TabSpec spec; // Resusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
LinearLayout ll = (LinearLayout)tabHost.getChildAt(0);
tw = (TabWidget)ll.getChildAt(1);
RelativeLayout tabIndicator1 = (RelativeLayout) LayoutInflater.from(this).inflate(R.layout.tab_indicator,tw,false);
title = (TextView)tabIndicator1.getChildAt(1);
icon = (ImageView)tabIndicator1.getChildAt(0);
intent = new Intent().setClass(this, HomeActivity.class);
title.setText("Index");
icon.setImageResource(R.drawable.info);
spec = tabHost.newTabSpec("Index").setIndicator(tabIndicator1).setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, TestActivity.class);
// 添加代码:
tabIndicator1 = (RelativeLayout) LayoutInflater.from(this).inflate(R.layout.tab_indicator,tw,false);
title = (TextView)tabIndicator1.getChildAt(1);
icon = (ImageView)tabIndicator1.getChildAt(0);
title.setText("Test");
icon.setImageResource(R.drawable.test);
spec = tabHost.newTabSpec("Test").setIndicator(tabIndicator1).setContent(intent);
tabHost.addTab(spec); // 正确运行