我使用兼容性包在我的 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>
我用于片段的相同代码可以正常工作而没有任何问题。我只面临片段的问题。这里有什么问题 ?有人告诉我解决方案。