【问题标题】:IllegalStateException when dynamically adding View to Layout将视图动态添加到布局时出现 IllegalStateException
【发布时间】:2014-03-28 01:50:31
【问题描述】:

嗨,我正在编写一个添加动态 testview 的程序。我想在单击按钮时添加一个 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>

请帮忙.. 提前致谢

【问题讨论】:

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


    【解决方案1】:

    您正在向 已经有父级的 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);
    

    【讨论】:

      【解决方案2】:

      您尝试添加的文本视图(tview)已经存在于您膨胀的布局(activity_main)中。它已经有一个父布局,无法再次添加

      而不是这个,你应该做一个

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

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

      【讨论】:

        【解决方案3】:

        您的 TextView "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);
        
                }
        

        【讨论】:

          【解决方案4】:

          IllegalStateException 发生是因为 TextView,textView,您尝试添加 layoutvs 的子级。您应该将vs 视图添加到layout 中。下面我已经更新了代码...检查一下。

          将以下监听器代码 sn-p 替换为您的。

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2016-06-05
            • 2011-06-18
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-11-17
            • 1970-01-01
            相关资源
            最近更新 更多