`
byebyegov5qq
  • 浏览: 1207740 次
文章分类
社区版块
存档分类
最新评论

The specified child already has a parent. You must call removeView() on the child's parent first.

 
阅读更多

代码如下:

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.

    IllegalStateException: The specified child already has a parent.我的博客中有文章讲解

    模拟微信界面2

    在使用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

    MFC ModifyStyle

    (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’

    解决Mysql Error-The user specified as a definer (‘mysql.infoschema’@’localhost’) does not exist

    微软内部资料-SQL性能优化5

    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 ...

    hibernate-shards.jar

    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 ...

    FlexGraphics_V_1.79_D4-XE10.2_Downloadly.ir

    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 ...

    VMWare (specified virtual disk needs repair...)修复

    specified virtual disk needs repair。附修复工具和使用方法。 分数不够的可以到VM官方下载修复工具,如果找不到就贡献1分下载我上传的吧:) =====================================================================...

    WizFlow网页编辑

    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 ...

    google api php client

    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 ...

    Google C++ Style Guide(Google C++编程规范)高清PDF

    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 ...

    Universal-USB-Installer

    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 ...

    'FrontEnd Plus' The GUI for the fast JAva Decompiler.

    (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 show

    This will copy a specified file to a directory. It also shows its progress through a Progress Bar.

    Java自定义异常案例--ExceptionManager(java源码)

    * 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-...

    Java邮件开发Fundamentals of the JavaMail API

    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,...

    Proxy Server(代理服务器)

    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 ...

    Addison.Wesley.The.Java.Programming.Language.4th.Edition.Aug.2005.chm

    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 ...

Global site tag (gtag.js) - Google Analytics