动态添加View到Layout时出现IllegalStateException

时间:2014-02-25 16:56:07

标签: android android-layout textview illegalstateexception android-inflate

您好我正在编写一个程序来添加动态testview.i想要在单击按钮时添加textview。但是当我添加textview时我收到错误

这是我的日志文件

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)

我的活动课

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>

请帮忙.. 提前致谢

4 个答案:

答案 0 :(得分:0)

您正在向已有父级的LinearLayout添加TextView。您的错误消息非常清楚。

您需要先从其父级布局中删除TextView,然后才能将其添加到其他布局。

这样做:

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

请注意,我只是假设“vs”是TextView的父布局。只需查看布局文件,即可获得TextView的父布局。然后致电:

parent.removeView(textView);

答案 1 :(得分:0)

您尝试添加的文本视图(tview)已经存在于您已膨胀的布局中(activity_main)。它不能再次添加,因为它已经有父布局

而不是这样你应该做一个

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

动态创建文本视图,然后将其添加到布局

答案 2 :(得分:0)

您的TextView“textView”已经存在并附加到View“vs”。

您无需为这样简单的操作充气xml。你可以这样做:

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

        }

答案 3 :(得分:0)

IllegalStateException正在发生,因为您尝试在TextView中添加的layout textView是vs的孩子。您应该将vs视图添加到layout。下面我已更新代码...请查看。

将以下侦听器代码段替换为您的。

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

        }
    });