gpt4 book ai didi

android - Android “The specified child already has a parent.” fragment onCreateView()吗?

转载 作者:行者123 更新时间:2023-12-02 10:48:45 25 4
gpt4 key购买 nike

我正在使用兼容性包在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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com