尝试通过循环动态添加多个textview时,应用程序崩溃。添加单个文本视图时,效果很好

时间:2018-07-05 16:30:40

标签: android android-studio

当我尝试通过以下代码动态添加多个文本视图时,Numbers Activity崩溃。但是当我不使用循环而是仅添加文本视图时,它可以很好地工作。

Numbers Activity.java

    public class NumbersActivity extends AppCompatActivity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_numbers);

        ArrayList<String> words = new ArrayList<String>();
        words.add("one");
        words.add("two");
        words.add("three");
        words.add("four");
        words.add("five");
        words.add("six");
        words.add("seven");
        words.add("eight");
        words.add("nine");
        words.add("ten");

        LinearLayout rootView = (LinearLayout) findViewById(R.id.rootView);
        rootView.removeAllViews();
        TextView  wordView = new TextView((this));
        int index;
        for(index=0; index<words.size(); index++ ){
            wordView.setText(words.get(index));
            rootView.addView(wordView);

        }
    }
}

activity_numbers.xml

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rootView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginLeft="16dp"
    android:orientation="vertical"
    tools:context=".NumbersActivity"
</LinearLayout>

应用崩溃后出现的错误是

  

E / Android运行时:致命异常:主要                     流程:com.example.yusuf.miwok,PID:12226                     java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.yusuf.miwok / com.example.yusuf.miwok.NumbersActivity}:   java.lang.IllegalStateException:指定的子对象已经有一个   父母您必须先在孩子的父母上调用removeView()。                         在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3194)                         在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3302)                         在android.app.ActivityThread.-wrap12(未知来源:0)                         在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1891)                         在android.os.Handler.dispatchMessage(Handler.java:108)                         在android.os.Looper.loop(Looper.java:166)                         在android.app.ActivityThread.main(ActivityThread.java:7425)                         在java.lang.reflect.Method.invoke(本机方法)                         在com.android.internal.os.Zygote $ MethodAndArgsCaller.run(Zygote.java:245)                         在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)                      原因:java.lang.IllegalStateException:指定的子代已经有一个父代。您必须在上调用removeView()   首先是孩子的父母。                         在android.view.ViewGroup.addViewInner(ViewGroup.java:4976)                         在android.view.ViewGroup.addView(ViewGroup.java:4807)                         在android.view.ViewGroup.addView(ViewGroup.java:4747)                         在android.view.ViewGroup.addView(ViewGroup.java:4720)                         在com.example.yusuf.miwok.NumbersActivity.onCreate(NumbersActivity.java:40)                         在android.app.Activity.performCreate(Activity.java:7372)                         在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1218)                         在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3147)                         在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3302)                         在android.app.ActivityThread.-wrap12(未知来源:0)                         在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1891)                         在android.os.Handler.dispatchMessage(Handler.java:108)                         在android.os.Looper.loop(Looper.java:166)                         在android.app.ActivityThread.main(ActivityThread.java:7425)                         在java.lang.reflect.Method.invoke(本机方法)                         在com.android.internal.os.Zygote $ MethodAndArgsCaller.run(Zygote.java:245)                         在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)   I / Process:正在发送信号。 PID:12226 SIG:9应用程序终止。

1 个答案:

答案 0 :(得分:3)

正确的代码如下

 public class NumbersActivity extends AppCompatActivity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_numbers);

        ArrayList<String> words = new ArrayList<String>();
        words.add("one");
        words.add("two");
        words.add("three");
        words.add("four");
        words.add("five");
        words.add("six");
        words.add("seven");
        words.add("eight");
        words.add("nine");
        words.add("ten");

        LinearLayout rootView = (LinearLayout) findViewById(R.id.rootView);
        rootView.removeAllViews();

        int index;
        for(index=0; index<words.size(); index++ ){
            TextView  wordView = new TextView((this));
            wordView.setText(words.get(index));
            rootView.addView(wordView);

        }
    }
}

请注意,我只是将此行移至for循环内

TextView  wordView = new TextView((this));