为什么我需要调用removeView(),以便于查看添加到我的LinearLayout [英] Why do I need to call removeView() in order to add a View to my LinearLayout

查看:294
本文介绍了为什么我需要调用removeView(),以便于查看添加到我的LinearLayout的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到此错误,但我不知道确切原因:

  java.lang.IllegalStateException:指定的小孩已经有一个父。您必须先对孩子的父母打电话removeView()。

在这里我想补充的观点是错误指向线的一部分。我提供我的适配器code为了让你们得到的我在做什么更好的画面,为什么我收到此错误。请让我知道是否需要的信息了。先谢谢了。

适配器

 私有类InnerAdapter延伸BaseAdapter {
    String []数组=新的String [] {12 \\南,1 \\南,2 \\南,3 \\南,4 \\南,5 \\ NAM
                                6 \\南,7 \\南,8 \\南,9 \\南,10 \\南,11 \\ NAM
                                12 \\故宫,1 \\故宫,2 \\故宫,3 \\故宫,4 \\故宫,5 \\故宫
                                6 \\故宫,7 \\故宫,8 \\故宫,9 \\故宫,10 \\故宫,11 \\故宫};
    TextView的[] =意见新的TextView [24];    公共InnerAdapter(){
        TextView中创建=新的TextView(DayViewActivity.this);
        LinearLayout.LayoutParams PARAMS =新LinearLayout.LayoutParams(0,(int)的TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,62,getResources()getDisplayMetrics()),1.0F);
        params.topMargin =(int)的TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,1,getResources()getDisplayMetrics());
        params.bottomMargin =(int)的TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,1,getResources()getDisplayMetrics());
        create.setLayoutParams(PARAMS);
        create.setBackgroundColor(Color.BLUE);
        create.setText(测试);
        意见[0] =创建;
        的for(int i = 1; I< views.length;我++){
            意见[我] = NULL;
        }
    }    @覆盖
    公众诠释的getCount(){
        // TODO自动生成方法存根
        返回array.length;
    }    @覆盖
    公共对象的getItem(INT位置){
        // TODO自动生成方法存根
        返回null;
    }    @覆盖
    众长getItemId(INT位置){
        // TODO自动生成方法存根
        返回的位置;
    }    @覆盖
    公共查看getView(INT位置,查看convertView,父母的ViewGroup){
        如果(convertView == NULL){
            LayoutInflater充气= LayoutInflater.from(parent.getContext());
            convertView = inflater.inflate(R.layout.day_view_item,父母,假);
        }        ((的TextView)convertView.findViewById(R.id.day_hour_side))的setText(数组[位置]);
        的LinearLayout布局=(的LinearLayout)convertView.findViewById(R.id.day_event_layout);
        如果(意见[位置]!= NULL){
            layout.addView((TextView的)观点[位置],位置);
        }        返回convertView;
    }}

XML

 < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =61dp
    机器人:方向=横向>    <的LinearLayout
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =61dp
        机器人:方向=垂直>
        <的TextView
            机器人:ID =@ + ID / day_hour_side
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =60dp
            机器人:文字=12AM
            机器人:背景=#bebebe
            机器人:layout_weight =0
            机器人:TEXTSIZE =10dp
            机器人:paddingLeft =5DP
            机器人:paddingRight =5DP/>
        <的TextView
            机器人:layout_width =match_parent
            机器人:layout_height =1DP
            机器人:layout_weight =0
            机器人:背景=#000000
            机器人:ID =@ + ID / hour_side_divider/>
    < / LinearLayout中>
    <的LinearLayout
        机器人:layout_width =0dp
        机器人:layout_height =61dp
        机器人:方向=垂直
        机器人:layout_weight =1>
        <的LinearLayout
            机器人:ID =@ + ID / day_event_layout
            机器人:layout_width =match_parent
            机器人:layout_height =60dp
            机器人:方向=横向>< / LinearLayout中>
        <的TextView
            机器人:layout_width =match_parent
            机器人:layout_height =1DP
            机器人:背景=#000000
            机器人:ID =@ + ID / event_side_divider/>
    < / LinearLayout中>
< / LinearLayout中>


解决方案

当你得到异常(你没说应用程序启动时,或当你滚动 GridView控件上下),但它是正常的。在的意见阵列有一个值不是(第一个数组中的<$ C条目集$ C>的TextView 您创建的),最有可能你会想重新添加的TextView 在一些点。此外父适配器视图可致电 getView 方法多次获得一些孩子来衡量。

反正你不知道你在想什么做的,但目前的做法是错误的。

首先,您创建一个数组有一个的TextView ,并设置为的价值和你的其余部分基本上不要做别的用它(但也许这是不是完整code?!)。其次,你不应该存储查看阵列尤其是在适配器视图的子元素(如 GridView控件,的ListView等),其中有一个机制来回收它的孩子。第三,你没有考虑采取 GridView控件的回收机制。例如,您添加的TextView 的第一个元素,但你不回复这个变化中的 getView 因此,如果这第一行的查看(其中包含新增的TextView )被回收,你会与行结束了查看包含行previously加入的TextView ,你不希望它

I am getting this error but I am not sure exactly why:

java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

The part where I add the View is the line that the error is pointing to. I am providing my Adapter code in order for you guys to get a better picture of what I am doing and why I am getting this error. Please let me know if anymore info is needed. Thanks in advance.

Adapter

private class InnerAdapter extends BaseAdapter{
    String[] array = new String[] {"12\nAM","1\nAM", "2\nAM", "3\nAM", "4\nAM", "5\nAM", 
                                "6\nAM", "7\nAM", "8\nAM", "9\nAM", "10\nAM", "11\nAM",
                                "12\nPM", "1\nPM", "2\nPM", "3\nPM", "4\nPM", "5\nPM",
                                "6\nPM", "7\nPM", "8\nPM", "9\nPM", "10\nPM", "11\nPM"};
    TextView[] views = new TextView[24];

    public InnerAdapter() {
        TextView create = new TextView(DayViewActivity.this);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 62, getResources().getDisplayMetrics()), 1.0f);
        params.topMargin = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1, getResources().getDisplayMetrics());
        params.bottomMargin = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1, getResources().getDisplayMetrics());
        create.setLayoutParams(params);
        create.setBackgroundColor(Color.BLUE);
        create.setText("Test");
        views[0] = create;
        for(int i = 1; i < views.length; i++) {
            views[i] = null;
        }
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return array.length;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if(convertView == null) {
            LayoutInflater inflater = LayoutInflater.from(parent.getContext());
            convertView = inflater.inflate(R.layout.day_view_item, parent, false);
        }

        ((TextView)convertView.findViewById(R.id.day_hour_side)).setText(array[position]);
        LinearLayout layout = (LinearLayout)convertView.findViewById(R.id.day_event_layout);
        if(views[position] != null) {
            layout.addView((TextView)views[position], position);
        }

        return convertView;
    }

}

XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="61dp"
    android:orientation="horizontal" >



    <LinearLayout 
        android:layout_width="wrap_content"
        android:layout_height="61dp"
        android:orientation="vertical">
        <TextView 
            android:id="@+id/day_hour_side"
            android:layout_width="wrap_content"
            android:layout_height="60dp"
            android:text="12AM"
            android:background="#bebebe"
            android:layout_weight="0"
            android:textSize="10dp"
            android:paddingLeft="5dp"
            android:paddingRight="5dp"/>
        <TextView 
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_weight="0"
            android:background="#000000"
            android:id="@+id/hour_side_divider"/>
    </LinearLayout>
    <LinearLayout 
        android:layout_width="0dp"
        android:layout_height="61dp"
        android:orientation="vertical"
        android:layout_weight="1">
        <LinearLayout 
            android:id="@+id/day_event_layout"
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:orientation="horizontal" ></LinearLayout>
        <TextView 
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="#000000"
            android:id="@+id/event_side_divider" />
    </LinearLayout>


</LinearLayout>

解决方案

You didn't say when you get that exception(when the application starts or when you scroll the GridView up and down) but it's normal. The views array has one value that is not null(the first entry in that array in set to the TextView that you create) and most likely you'll be trying to re-add that TextView at some point. Also the parent AdapterView may call the getView method several times to get some children to measure.

Anyway you don't know exactly what you're trying to do but the current approach is wrong.

First, you create an array with one TextView and the rest of the values set to null and you basically don't do anything else with it(but maybe this isn't the full code?!). Second, you shouldn't store an array of Views especially within a child of AdapterView(like GridView, ListView etc) which has a mechanism to recycle its children. Third, you didn't take in consideration the recycling mechanism of the GridView. For example, you add the TextView for the first element but you don't revert this changes in the getView so if this first row's View(which contains the added TextView) gets recycled you'll end up with a row View containing the previously added TextView at rows where you don't want it.

这篇关于为什么我需要调用removeView(),以便于查看添加到我的LinearLayout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆