Android “The specified child already has a parent.” for Fragments onCreateView()?

浪尽此生 提交于 2019-12-24 05:53:00

问题


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

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);
}

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;         
}

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>

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.


回答1:


I think somethings wrong in this part

  listview.setLayoutParams(add_lp);

it should be

 addView.setLayoutParams(add_lp);   


来源:https://stackoverflow.com/questions/15194618/android-the-specified-child-already-has-a-parent-for-fragments-oncreateview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!