指定的子项已有父项。您必须首先在孩子的父母上调用removeView(),然后再显示错误

时间:2015-02-10 20:17:57

标签: android fragment android-listfragment

当我运行程序时,我收到错误E / AndroidRuntime(27275):java.lang.IllegalStateException:指定的子项已经有父项。您必须首先在孩子的父母上调用removeView()。问题在哪里?

我的列表片段类

    package com.zaa.yhgj;        
    public class HomeFragment extends ListFragment {
        private List<ListViewItem> mItems;        // ListView items list
        View myFragmentView;
        ArrayList<View> chart_view = new ArrayList<View>();
        List<Integer> onedimen = new ArrayList<Integer>();
        List<List> aa = new ArrayList<List>();
        ///
        List<List> statuslistoflist = new ArrayList<List>();
        List<String> statusonedimen = new ArrayList<String>();

        private List<DialogInterface.OnClickListener> nItems;


     @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
             myFragmentView = inflater.inflate(R.layout.dashboardlist, container, false);


            HashMap<String,Integer> myhash=new HashMap<String, Integer>() ;
            myhash.put("Pending",0);
            myhash.put("In Progress",0);
            myhash.put("Limitation",0);
            myhash.put("Needs Research",0);
            myhash.put("In Testing",0);
            myhash.put("Issue Not Clear",0);
            myhash.put("Unassigned",0);
            int mainclasssize = Constants.listuserprojectMain.getMainListClass().size();
            int[] listsize = new int[mainclasssize];
            String[] projectname = new String[mainclasssize];
            int[] openissuecount = new int[mainclasssize];

            for (int k = 0; k < mainclasssize; k++) {

                listsize[k] = Constants.listuserprojectMain.getMainListClass().get(k).getProjectIssue().size();
                projectname[k] = Constants.listuserprojectMain.getMainListClass().get(k).getName();
                openissuecount[k] = Constants.listuserprojectMain.getMainListClass().get(k).getOpenIssueCount();
            }


            for (int w = 0; w < mainclasssize; w++) {

                onedimen = new ArrayList<Integer>();

                for (int x = 0; x < listsize[w]; x++) {
                    myhash.put(Constants.listuserprojectMain.getMainListClass().get(w).getProjectIssue().get(x).getStatus(),Constants.listuserprojectMain.getMainListClass().get(w).getProjectIssue().get(x).getIssueCount());


                }

                Set keys = myhash.keySet();
                Iterator itr = keys.iterator();

                String key;
                int value;
                while(itr.hasNext())
                {
                    key = (String)itr.next();
                    onedimen.add((Integer)myhash.get(key));
                    value = (Integer)myhash.get(key);
                    // System.out.println(key + " - "+ value);
                }
                aa.add(onedimen);
                statuslistoflist.add(statusonedimen);


            }
            for (int v = 0; v < aa.size(); v++) {


                chart_view.add(openChart(aa.get(v)));

            }
            mItems = new ArrayList<ListViewItem>();
            nItems = new ArrayList<DialogInterface.OnClickListener>();
            Resources resources = getResources();
            for (int f = 0; f < aa.size(); f++) {
                mItems.add(new ListViewItem(projectname[f], openissuecount[f], chart_view.get(f)));
            }

            nItems.add(new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {

                }
            });


            // initialize and set the list adapter

            setListAdapter(new ListViewDemoAdapter(getActivity(), mItems));



            return myFragmentView;
        }

        private View openChart(List<Integer> chartvaluedd) {




            return v;

        }

    }

我的适配器类是         公共类ListViewDemoAdapter扩展了ArrayAdapter {

    public ListViewDemoAdapter(Context context, List<ListViewItem> items) {
            super(context, R.layout.dashboardlistview_item, items);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
           ViewHolder viewHolder=  new ViewHolder();

            if(convertView == null) {
                // inflate the GridView item layout
                LayoutInflater inflater = LayoutInflater.from(getContext());
                convertView = inflater.inflate(R.layout.dashboardlistview_item, parent, false);

                // initialize the view holder

               // viewHolder.ivIcon = (ImageView) convertView.findViewById(R.id.ivIcon);
                viewHolder.graphView = (LinearLayout) convertView.findViewById(R.id.ll_chart);
                viewHolder.tvCreateIssue = (TextView) convertView.findViewById(R.id.tvCreateissue);
               viewHolder.tvDetails = (TextView) convertView.findViewById(R.id.tv_details);
                viewHolder.tvIssue = (TextView) convertView.findViewById(R.id.tv_issues);
                viewHolder.tvProjectTitle = (TextView) convertView.findViewById(R.id.tv_projectTitle);
                viewHolder.tvOpenIssueCount = (TextView) convertView.findViewById(R.id.tv_openissues);
                convertView.setTag(viewHolder);

            } else {
                viewHolder = (ViewHolder) convertView.getTag();
                // recycle the already inflated view

                ///error when scrolling graph
                viewHolder.graphView.removeAllViews();


            }

            // update the item view
            final ListViewItem item = getItem(position);

          viewHolder.tvCreateIssue.setText("Create Issue");
            viewHolder.tvProjectTitle.setText(item.title);


                viewHolder.graphView.addView(item.chartview);

         viewHolder.tvOpenIssueCount.setText("Open Issues "+item.openissuecount);




            viewHolder.tvCreateIssue.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(getContext(), item.title, Toast.LENGTH_SHORT).show();
                }
            });
            /////

            viewHolder.tvIssue.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Toast.makeText(getContext(),"issue",Toast.LENGTH_LONG).show();

                }
            });
            return convertView;
        }

        private static class ViewHolder {
           // ImageView ivIcon;
            LinearLayout graphView;
            TextView tvCreateIssue;
            TextView tvDetails;
            TextView tvProjectTitle;
            TextView tvOpenIssueCount;
            TextView tvIssue;
        }
    }

我的xml布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <include layout="@layout/top" />

    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"


        android:scrollbars="none" >
    </ListView>

</LinearLayout>

我的listview项目是

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:gravity="center_vertical"
    android:background="@color/list_background"
    android:padding="5dp"
    android:id="@+id/ww"
    >

    <!-- the innner view - provides the white rectangle -->
    <RelativeLayout android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:background="@drawable/frame"
        android:id="@+id/jhhh">

        <!-- the icon view -->
        <TextView
            android:id="@+id/tv_projectTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Project name"
            android:textSize="16dp"
            android:textStyle="bold"
            android:layout_centerHorizontal="true"
            />
        <LinearLayout android:id="@+id/ll_chart"
            android:orientation="vertical"
            android:layout_below="@+id/tv_projectTitle"
            android:layout_width="wrap_content"
            android:layout_height="230dp"
            android:background="#ffffff"
            android:padding="5dp"


            android:scaleType="fitXY" />
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/ll_chart"
    android:id="@+id/tv_openissues"
    android:text="Open Issues 24"/>

     <LinearLayout
         android:layout_width="wrap_content"
         android:id="@+id/ll_textview"
         android:padding="5dp"
         android:layout_alignParentRight="true"
         android:layout_below="@id/tv_openissues"
         android:orientation="horizontal"
         android:layout_height="wrap_content">
            <TextView android:id="@+id/tvCreateissue"
                android:drawableLeft="@drawable/bug6"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"

                android:text="Create Issue"
                 />

            <!-- the description view -->
            <TextView android:id="@+id/tv_details"
            android:layout_below="@id/tvCreateissue"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
                android:drawableLeft="@drawable/bars64"
                android:layout_marginLeft="5dp"
            android:text="Details"
             />
            <TextView android:id="@+id/tv_issues"
                android:layout_below="@id/tvCreateissue"
                android:layout_marginLeft="5dp"
                android:drawableLeft="@drawable/bug6"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Issues"
                />
     </LinearLayout>
        </RelativeLayout>

    </RelativeLayout>

2 个答案:

答案 0 :(得分:1)

我可能错了,但错误似乎就在这里:

viewHolder.graphView.addView(item.chartview);

将视图item.chartview添加到graphView后,它会有父级。但是,已知ListView多次在某些位置调用getView进行缓存。

似乎相同的视图被添加到多个父母,这反过来又会引发你得到的错误。在添加chartview之前,您可以尝试将其从父项中删除(如果存在):

// Remove from parent (if exists)
ViewGroup parent = item.chartview.getParent();
if (parent != null) {
    parent.removeView(item.chartview);
}
// Add to another parent
viewHolder.graphView.addView(item.chartview);

答案 1 :(得分:0)

 public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
             myFragmentView = inflater.inflate(R.layout.dashboardlist, container, false);

而不是容器使用null (我不是一个机器怪人,但之前遇到过这个问题)