代码如下:
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 =newIntent().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
Resourcesres= getResources();// Resource object to getDrawables
TabHost tabHost = getTabHost();// The activity TabHost
TabHost.TabSpec spec;//ResusableTabSpec 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 =newIntent().setClass(this, HomeActivity.class);
title.setText("Index");
icon.setImageResource(R.drawable.info);
spec = tabHost.newTabSpec("Index").setIndicator(tabIndicator1).setContent(intent);
tabHost.addTab(spec);
intent =newIntent().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); // 正确运行
分享到:
相关推荐
IllegalStateException: The specified child already has a parent.我的博客中有文章讲解
在使用ViewPager + Fragment 的时候报错:The specified child already has a parent. You must call removeView() on the child’s parent first.
在使用ViewPager + Fragment 的时候报错:The specified child already has a parent. You must call removeView() on the child’s parent first
(If WS_CLIPSIBLINGS is not given and child windows overlap, when you draw within the client area of a child window, it is possible to draw within the client area of a neighboring child window.) For ...
解决Mysql Error-The user specified as a definer (‘mysql.infoschema’@’localhost’) does not exist
On a qualified select, update, or delete, the correct leaf page will be the lowest page of the tree in which one or more rows with the specified key or keys reside. A qualified operation is one that ...
or for a fee, you must give the recipients all the rights that we gave you. You must make sure that they, too, receive or can get the source code. If you link other code with the library, you must ...
on a curve by the distance from the original point on the perimeter. - ADD: Added the method TCustomProp.GetPublishedComplexProp - returns included complex flex-property by name registered through ...
specified virtual disk needs repair。附修复工具和使用方法。 分数不够的可以到VM官方下载修复工具,如果找不到就贡献1分下载我上传的吧:) =====================================================================...
or for a fee, you must give the recipients all the rights that we gave you. You must make sure that they, too, receive or can get the source code. If you link other code with the library, you must ...
The method used is a matter of preference, but *it will be very difficult to use this library without first understanding the JSON syntax for the API*, so it is recommended to look at the [APIs ...
On the other hand, you must include the header file for Foo if your class subclasses Foo or has a data member of type Foo. Sometimes it makes sense to have pointer (or better, scoped_ptr) members ...
gratis or for a fee, you must give the recipients all the rights that you have. You must make sure that they, too, receive or can get the source code. And you must show them these terms so they know ...
(or jad -o -d test -s java *.class, which has the same effect) This command decompiles all .class files in the current directory and places all output files with extension .java into directory '...
This will copy a specified file to a directory. It also shows its progress through a Progress Bar.
* Show the information in a dialog which has the specified title and belongs to the * specified component. * * @param parent The component cause the exception. * @param title The title of ...
12. A circuit-switched network can guarantee a certain amount of end-to-end bandwidth for the duration of a call. Most packet-switched networks today (including the Internet) cannot make any end-to-...
SMTP server will relay the message on to the SMTP server of the recipient(s) to eventually be acquired by the user(s) through POP or IMAP. This does not require your SMTP server to be an open relay,...
Once a child has been forked, the parent resumes listening for additional connections. The name of the isolated host and the port to serve as proxy for, as well as the port number the ...
and the impact this has on some of the new language features. <br>Appendix BUseful Tableshas tables of information that you may find useful for quick reference. <br>Finally, Further Reading ...