Android尝试在TableLayout programmaticaly中添加TableRow时获取非法状态异常

时间:2017-06-07 21:51:04

标签: android textview tablelayout illegalstateexception

我有一张桌子和桌子,有5个textview。我在myfragment.xml中创建了tablelayout和tablerow,我想将tablerow添加到tablelayout programmaticaly。但我整天都有非法国家例外。

这是我的table和tablerow xml代码(myfragment.xml):

<LinearLayout tools:context="com.xxx.xx.Fragment.myfragment"xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"
android:layout_height="match_parent" android:layout_width="match_parent"
android:orientation="vertical" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/lineer">

            <!-- a Spinner -->
            <!-- a Textview -->

            <TableLayout
                android:id="@+id/mytable"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:padding="5dip"
                android:shrinkColumns="*">

                <TableRow android:id="@+id/rowtest"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    xmlns:android="http://schemas.android.com/apk/res/android">
                    <TextView
                        android:id="@+id/table_one"
                        android:layout_weight="1" />
                    <TextView
                        android:id="@+id/table_two"
                        android:layout_weight="1" />
                    <TextView
                        android:id="@+id/table_three"
                        android:layout_weight="1"/>
                    <TextView
                        android:id="@+id/table_four"
                        android:layout_weight="1"/>
                    <TextView
                        android:id="@+id/table_five"
                        android:layout_weight="1" />
                </TableRow>
            </TableLayout>
        </LinearLayout>

我想动态创建tablerow,并在用户从微调器中选择内容时将其添加到tablelayout。但是当我从Spinner那里选择一些东西时,我会得到非法状态异常。我认为我的文本视图正在创造这个问题。这是android代码:

TextView tv,tv2,tv3,tv4,tv5;
TableLayout detailsTable;
TableRow tableRow;
@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view =  inflater.inflate(R.layout.myfragment, container, false);

        detailsTable = (TableLayout) view.findViewById(R.id.mytable);
        tableRow = (TableRow) view.findViewById(R.id.rowtest); 

        tv = (TextView) view.findViewById(R.id.table_one);
        tv2 = (TextView) view.findViewById(R.id.table_two);
        tv3 = (TextView) view.findViewById(R.id.table_three);
        tv4 = (TextView) view.findViewById(R.id.table_four);
        tv5 = (TextView) view.findViewById(R.id.table_five);
        return view;
}

我在spinner的onitemselected()方法中调用我的createrow()方法。我有一个createrow()方法:

private void createRow(String da, String hn,String krd,String puan, String snc) {
        tv.setText(da);
        denemerow.addView(tv);

        tv2.setText(hn);
        denemerow.addView(tv2);

        tv3.setText(krd);
        denemerow.addView(tv3);

        tv4.setText(puan);
        denemerow.addView(tv4);

        tv5.setText(snc);
        denemerow.addView(tv5);

        detailsTable.addView(denemerow);
    }

我的logcat:

    java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
                                                                                     at android.view.ViewGroup.addViewInner(ViewGroup.java:3880) 
                                                                                at android.view.ViewGroup.addView(ViewGroup.java:3733)         

at android.view.ViewGroup.addView(ViewGroup.java:3678)                                                                                 

at android.view.ViewGroup.addView(ViewGroup.java:3654)                                                                                 

at com.xx.xx.Fragment.myfragment.createRow(myfragment.java:284)                                                                                 

at com.xx.xx.Fragment.myfragment.onItemSelected(myfragment.java:356)                                                                                 

at android.widget.AdapterView.fireOnSelected(AdapterView.java:897)                                                                                 

at android.widget.AdapterView.access$200(AdapterView.java:48)                                                                                 

at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:865)                                                                                 

at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)                                

at android.os.Looper.loop(Looper.java:135)                 
    at android.app.ActivityThread.main(ActivityThread.java:5272)                             

at java.lang.reflect.Method.invoke(Native Method)                              

at java.lang.reflect.Method.invoke(Method.java:372)                                                                                 

at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:909)                                                                               

at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:704)

我失踪的地方?

0 个答案:

没有答案