Android和QUOT;指定的小孩已经有一个父&QUOT。对于片段onCreateView()? [英] Android "The specified child already has a parent." for Fragments onCreateView()?

查看:110
本文介绍了Android和QUOT;指定的小孩已经有一个父&QUOT。对于片段onCreateView()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是兼容包用我的Andr​​oid应用程序SherlockFragments。收到此错误的片段致命异常:主要
java.lang.IllegalStateException:指定的子已具有父。您必须先对孩子的父母打电话removeView()
我加入这些片段在动作条福尔摩斯标签。运行后,应用程序成功,当我切换标签第一次它的正常工作,在第二次它给在onCreateView)上面的错误(..

I'm using SherlockFragments in my Android application using the compatibility package. Getting this error for fragments. FATAL EXCEPTION: main java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. I am adding these fragments as tabs in actionbar sherlock. After run the app successfully when i am switching tabs first time it's working fine, in second time it's giving the above error in onCreateView()..

这是对的onCreate code()

this is the code in onCreate()

private static final String AD_UNIT_STANDARD_BANNER = "/6253334/dfp_example_ad/banner";
private DfpAdView adView;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState); 

    adView = new DfpAdView(getActivity(), AdSize.BANNER, AD_UNIT_STANDARD_BANNER);  
    adView.setAdListener(this);
}

这是code,我使用的片段onCreateView()。

this is the code i am using in fragment onCreateView().

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {     
    View view = inflater.inflate(R.layout.fragment_chats, container, false);         
    listview = (ListView) view.findViewById(android.R.id.list); 
    RelativeLayout layout = (RelativeLayout) view.findViewById(R.id.chats_inner_layout);

    adView.setId(111);
    adView.loadAd(new AdRequest());        
    RelativeLayout.LayoutParams add_lp = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    add_lp.addRule(RelativeLayout.BELOW, adView.getId());   
    listview.setLayoutParams(add_lp);

    //**** this is the line causing for error****/////
    layout.addView(adView);         
    return view;         
}

这是XML code以上片段

this is the xml code for above fragment

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/chats_main_layout"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:background="@color/app_lite_theme"
   android:orientation="vertical" >

   <RelativeLayout
      android:id="@+id/chats_inner_layout"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical" >

      <ListView
          android:id="@android:id/list"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:layout_marginLeft="8dp"
          android:layout_marginRight="8dp"
          android:scrollbars="none" />

   </RelativeLayout>
</RelativeLayout>

这同样code,我使用的是没有任何问题工作正常片段。我面临的问题,只有片段。这里有什么问题吗?有人告诉
 我的解决方案这一点。

this same code i am using for fragments that is working fine without any problems. i am facing problem with fragments only. What is the problem here ? anybody tell me solution for this.

推荐答案

我想出头错在这部分

  listview.setLayoutParams(add_lp);

应该是

 addView.setLayoutParams(add_lp);   

这篇关于Android和QUOT;指定的小孩已经有一个父&QUOT。对于片段onCreateView()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆