android - Android “The specified child already has

我正在使用兼容性包在Android应用程序中使用SherlockFragments。获取fragments的此错误。 FATAL EXCEPTION: main java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.我正在将这些片段作为标签添加到Actionbar Sherlock中。当我第一次切换选项卡成功运行应用程序后,它运行良好,第二次在onCreateView()中给出了以上错误。

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

这是我在片段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代码
<?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>

我正在使用相同的代码片段正常工作,没有任何问题。我只面对碎片问题。这里有什么问题 ?有人告诉
我为此解决。

最佳答案

我认为这部分出了问题

  listview.setLayoutParams(add_lp);

它应该是
 addView.setLayoutParams(add_lp);   

关于android - Android “The specified child already has a parent.” fragment onCreateView()吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15194618/

相关文章:

android - 如何解决Java Eclipse中的 “The hierarchy of the

android - 如何解决java.lang.NoClassDefFoundError

compiler-construction - 未定义对ruserok错误的引用

visual-c++ - 如何针对VES2008编译Greta?

asp.net - asp.net C#编译器错误CS1061

perl - 无法编译我的 perl 脚本。让一切正常工作的最简单方法是什么?

vba - 具有多个条件的Select Case函数不循环

android - 生成的渲染脚本文件导致 “Invalid unicode”错误

xcode - Intel C++编译器,无法在OS X上打开stdarg.h

windows - Windows上的powerpc编译器给出 “c++ compiler not