Android 向布局动态添加视图时出现IllegalStateException

Android 向布局动态添加视图时出现IllegalStateException,android,android-layout,textview,illegalstateexception,android-inflate,Android,Android Layout,Textview,Illegalstateexception,Android Inflate,您好,我正在编写一个程序来添加动态测试视图。我想在单击按钮时添加一个文本视图。但是当我添加文本视图时,我遇到了错误 这是我的日志文件 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:

您好,我正在编写一个程序来添加动态测试视图。我想在单击按钮时添加一个文本视图。但是当我添加文本视图时,我遇到了错误

这是我的日志文件

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

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

请帮忙。。
提前感谢

您正在将文本视图添加到已具有父级的线性布局中。您的错误消息非常清楚

在将文本视图添加到另一个布局之前,需要将其从父布局中删除。

这样做:

 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);
您尝试添加的文本视图(tview)已存在于您已膨胀的布局中(activity_main)。无法再次添加,因为它已具有父布局

相反,你应该做一个

TextView TextView=newtextView(v.getContext())

要动态创建文本视图,然后将其添加到布局中,您的文本视图“TextView”已存在,并已附加到视图“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);

        }

发生
IllegalStateException
是因为您尝试在
layout
中添加的
TextView
vs
的子项。您应该将
vs
视图添加到
布局中。下面我已经更新了代码…请查看

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

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

        }
    });