0

hi i am writing a program to add dynamic testview.i want to add a textview when clicking a button.But when i add textview i am getting error

This is my log file

02-25 22:13:08.835: W/dalvikvm(3609): threadid=1: thread exiting with uncaught exception (group=0x40f102a0)
02-25 22:13:08.843: E/AndroidRuntime(3609): FATAL EXCEPTION: main
02-25 22:13:08.843: E/AndroidRuntime(3609): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
02-25 22:13:08.843: E/AndroidRuntime(3609):     at android.view.ViewGroup.addViewInner(ViewGroup.java:3387)
02-25 22:13:08.843: E/AndroidRuntime(3609):     at android.view.ViewGroup.addView(ViewGroup.java:3258)
02-25 22:13:08.843: E/AndroidRuntime(3609):     at android.view.ViewGroup.addView(ViewGroup.java:3234)
02-25 22:13:08.843: E/AndroidRuntime(3609):     at com.example.test.MainActivity$1.onClick(MainActivity.java:37)
02-25 22:13:08.843: E/AndroidRuntime(3609):     at android.view.View.performClick(View.java:4222)
02-25 22:13:08.843: E/AndroidRuntime(3609):     at android.view.View$PerformClick.run(View.java:17273)
02-25 22:13:08.843: E/AndroidRuntime(3609):     at android.os.Handler.handleCallback(Handler.java:615)
02-25 22:13:08.843: E/AndroidRuntime(3609):     at android.os.Handler.dispatchMessage(Handler.java:92)
02-25 22:13:08.843: E/AndroidRuntime(3609):     at android.os.Looper.loop(Looper.java:137)
02-25 22:13:08.843: E/AndroidRuntime(3609):     at android.app.ActivityThread.main(ActivityThread.java:4895)
02-25 22:13:08.843: E/AndroidRuntime(3609):     at java.lang.reflect.Method.invokeNative(Native Method)
02-25 22:13:08.843: E/AndroidRuntime(3609):     at java.lang.reflect.Method.invoke(Method.java:511)
02-25 22:13:08.843: E/AndroidRuntime(3609):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)
02-25 22:13:08.843: E/AndroidRuntime(3609):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
02-25 22:13:08.843: E/AndroidRuntime(3609):     at dalvik.system.NativeStart.main(Native Method)

My activity class

import android.os.Bundle;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class MainActivity extends Activity {

    Button btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn=(Button)findViewById(R.id.button);
        btn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                LinearLayout layout=(LinearLayout)findViewById(R.id.rlayout);
                   LayoutInflater mInflater = (LayoutInflater) getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
                   View  vs= (View) mInflater.inflate(R.layout.test, null);
                TextView textView = (TextView) vs.findViewById(R.id.tview);
                textView.setText("your text");
                LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.WRAP_CONTENT,
                        LinearLayout.LayoutParams.WRAP_CONTENT);
                layout.addView(textView,p);

            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

actvity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" 
    android:id="@+id/rlayout">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <Button 
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</LinearLayout>

test.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:id="@+id/test" >

          <TextView 
              android:id="@+id/tview"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"/>


</LinearLayout>

please help.. Thanks in advance

Hamid Shatu
  • 9,664
  • 4
  • 30
  • 41
user2298758
  • 47
  • 2
  • 6
  • 14

4 Answers4

0

You are adding a TextView to a LinearLayout that already has a parent. Your error message is perfectly clear.

You need to remove the TextView from it's parent layout before you can add it to another layout.

Do it like this:

 TextView textView = (TextView) vs.findViewById(R.id.tview);
 vs.removeView(textView); // assuming "vs" is the parent layout of your TextView

 LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.WRAP_CONTENT,
                        LinearLayout.LayoutParams.WRAP_CONTENT);

 LinearLayout layout = (LinearLayout)findViewById(R.id.rlayout);
 layout.addView(textView,p);

Please not that I am only making the assumption that "vs" is the parent layout of your TextView. Just look up in your layout file, and get the parent layout of your TextView. Then call:

parent.removeView(textView);
Philipp Jahoda
  • 50,880
  • 24
  • 180
  • 187
0

The text view(tview) which you are trying to add is already present in the layout which you have inflated (activity_main). It cannot be added again as it already has a parent layout

Instead of this you should be doing a

TextView textView = new TextView(v.getContext());

to dynamically create a text view and then add it to the layout

Ajit Pratap Singh
  • 1,299
  • 12
  • 24
0

Your TextView "textView" already exists and is attached to the View "vs".

You don't need to inflate an xml for such a simple operation. You could do something like:

@Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            LinearLayout layout=(LinearLayout)findViewById(R.id.rlayout);
            TextView textView = new TextView(MainActivity.this);
            textView.setText("your text");
            LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.WRAP_CONTENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT);
            layout.addView(textView,p);

        }
Showpath
  • 678
  • 1
  • 8
  • 18
0

The IllegalStateException is happening because the TextView, textView, you are trying to add in layout is the child of vs. You should add the vs view into the layout.Below I have updated the code...check it out.

Replace the following listener code snippet with yours.

    btn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            LinearLayout layout=(LinearLayout)findViewById(R.id.rlayout);
            LayoutInflater mInflater = (LayoutInflater) getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

            View  vs= (View) mInflater.inflate(R.layout.test, null);
            TextView textView = (TextView) vs.findViewById(R.id.tview);
            textView.setText("your text");

            layout.addView(vs);

        }
    });
Hamid Shatu
  • 9,664
  • 4
  • 30
  • 41