0

I have a table and tablerow which has 5 textview. I created tablelayout and tablerow in myfragment.xml and I want to add tablerow to tablelayout programmaticaly. But I am got Illegal State Exception during all day.

Here is my table and tablerow xml code(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>

I want to create tablerow dynamically and add it to tablelayout when user choose something from spinner. But I am getting Illegal State Exception when I choose something from Spinner. I think my textviews are creating this problem. Here is android codes:

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;
}

I call my createrow() method in spinner's onitemselected() method. I have a createrow() method:

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);
    }

My 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)

Where I am missing?

pseudocode
  • 209
  • 1
  • 6
  • 17
  • That's not a `NullPointerException`. It's an `IllegalStateException`. Those `TextView`s are already attached to a `View`. You can't add them again unless you remove them from their parent first, which is what the Exception message is telling you. If you want to create a new row, you have to create new `View`s. – Mike M. Jun 07 '17 at 22:01
  • @MikeM. Can you write an example ? – pseudocode Jun 07 '17 at 22:14
  • [Here's one](https://stackoverflow.com/a/8132212) that shows how to define a `TableRow` in a separate layout, inflate it, find and set the `TextView`s, then add it as a whole to the `TableLayout`. [Here's one](https://stackoverflow.com/a/33017697) that shows how to do everything in code, without the separate row layout. – Mike M. Jun 07 '17 at 22:21
  • 1
    The first one worked. Thank you so much :) @MikeM. – pseudocode Jun 07 '17 at 22:41

0 Answers0