【问题标题】:Android The specified child already has a parent. You must call removeView() on the child's parent firstAndroid 指定的孩子已经有一个父母。您必须先在孩子的父母上调用 removeView()
【发布时间】:2015-12-31 08:06:32
【问题描述】:

我正在创建一个应用程序,其中图像显示在水平 ScrollView 中。
我从 URI 中获取图像,我在下面的代码中提到。

我收到了异常The specified child already has a parent. You must call removeView() on the child's parent first.

请告诉我这是我的错误

 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.category_subcat_show, container, false);
    txtSubCatName = (TextView) rootView.findViewById(R.id.suc_cat_txt);
    SubCAtLyt = (LinearLayout) rootView.findViewById(R.id.sub_cat_layout);
    tf = Typeface.createFromAsset(getActivity().getAssets(), "fonts/SourceSansPro-Regular.otf");
    groupCategoryPosition = getArguments().getInt("groupPosition", 0);
    String catPosition = groupCategoryPosition + 1 + "";
    try {
        subCatDetails = db.getSubCatName(catPosition);
        int subCatlength = subCatDetails.size();

        scrollViewCatBooks =  new HorizontalScrollView(getActivity());
        View v1 = null;
        LinearLayout linear_pmain = new LinearLayout(getActivity());
        linear_pmain.setOrientation(LinearLayout.HORIZONTAL);
        LayoutInflater vii = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v1 = vii.inflate(R.layout.sub_bottomthubnail, null);
        for (int i = 0; i < subCatlength; i++) {
            List<String> listitem = subCatDetails.get(i);
            subCatid = listitem.get(0);
            subCatNanme = listitem.get(1);
            catId = listitem.get(2);
            txtSubCatName.setText(subCatNanme);
            arrCatAllBookDetails = db.getAllBook(catId, subCatid);
            int catAllLength = arrCatAllBookDetails.size();
            for (int j = 0; j < catAllLength; j++) {
                List<String> listAllBookDetails = arrCatAllBookDetails.get(j);
                bookPrise = listAllBookDetails.get(10);
                imgUrl = listAllBookDetails.get(13);

                subCatImage = (ImageView) v1.findViewById(R.id.imggSub);
                TextView txt = (TextView) v1.findViewById(R.id.txtg1);
                txt.setText("Rs." + bookPrise + "/-");
                txt.setTypeface(tf, Typeface.NORMAL);

                imageLoader = new ImageLoader(getActivity());
                imageLoader.DisplayImage(imgUrl, getActivity(), subCatImage);
                linear_pmain.addView(v1);
            }
            /*subCatImage.setTag(new String[]{listitem.get(0), listitem.get(1), listitem.get(2), "subcategory"});
            */
            //img.setOnTouchListener(mytochListener);
            // subCatImage.setOnClickListener(myIItemClickListener);
        }
        scrollViewCatBooks.addView(linear_pmain);
        SubCAtLyt.addView(scrollViewCatBooks);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return rootView;
}

我的 xml

  <?xml version="1.0" encoding="utf-8"?>

      <LinearLayout    xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical" 
 android:background="@drawable/noolreaderbackground">
 <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@drawable/noolreaderbackground">

    <RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="50dp"
    android:id="@+id/Rrlyt"
    android:gravity="center_vertical" >
    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:id="@+id/Rrlyt1"
        android:gravity="center_vertical" >
    <TextView
        android:id="@+id/suc_cat_txt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginLeft="20dp"
        android:text="Subcategory Names"
        android:textColor="@color/white"
        android:textSize="18dp" />

      <ImageView
        android:id="@+id/recommendedimg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="20dp"
        android:background="@drawable/viewall_selector" />
    </RelativeLayout>

   </RelativeLayout>
    <LinearLayout
        android:id="@+id/sub_cat_layout"
        android:layout_width="fill_parent"
        android:layout_below="@id/Rrlyt1"
        android:layout_height="100dp"
        android:orientation="horizontal"
        >
    </LinearLayout>
    </LinearLayout>
   </LinearLayout>

【问题讨论】:

  • 请发布您的 XML
  • 这里我在水平 scollview @FrankN.Stein 中显示图像
  • 哪里出错了?发布日志猫
  • linear_pmain.addView(v1);这一行@RRR

标签: android android-fragments horizontal-scrolling universal-image-loader


【解决方案1】:

将“false”作为充气器中的第三个参数传递,它将解决问题。此 false 不会将视图附加到根。

    v1 = vii.inflate(R.layout.sub_bottomthubnail, null, false);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-10-18
    • 2015-03-20
    • 2015-09-06
    • 2020-02-16
    • 2014-06-02
    相关资源
    最近更新 更多