gpt4 book ai didi

android - 尝试通过循环动态添加多个 TextView 时应用程序崩溃。添加单个 TextView 时效果很好

转载 作者:搜寻专家 更新时间:2023-11-01 08:20:58 24 4
gpt4 key购买 nike

当我尝试通过以下代码动态添加多个 TextView 时,Numbers Activity 崩溃了。但是当我不使用循环而只是添加 TextView 时它工作正常。

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/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.yusuf.miwok, PID: 12226 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.yusuf.miwok/com.example.yusuf.miwok.NumbersActivity}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3194) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3302) at android.app.ActivityThread.-wrap12(Unknown Source:0) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1891) at android.os.Handler.dispatchMessage(Handler.java:108) at android.os.Looper.loop(Looper.java:166) at android.app.ActivityThread.main(ActivityThread.java:7425) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921) Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. at android.view.ViewGroup.addViewInner(ViewGroup.java:4976) at android.view.ViewGroup.addView(ViewGroup.java:4807) at android.view.ViewGroup.addView(ViewGroup.java:4747) at android.view.ViewGroup.addView(ViewGroup.java:4720) at com.example.yusuf.miwok.NumbersActivity.onCreate(NumbersActivity.java:40) at android.app.Activity.performCreate(Activity.java:7372) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1218) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3147) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3302)  at android.app.ActivityThread.-wrap12(Unknown Source:0)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1891)  at android.os.Handler.dispatchMessage(Handler.java:108)  at android.os.Looper.loop(Looper.java:166)  at android.app.ActivityThread.main(ActivityThread.java:7425)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)  I/Process: Sending signal. PID: 12226 SIG: 9 Application terminated.

最佳答案

正确的代码如下

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

关于android - 尝试通过循环动态添加多个 TextView 时应用程序崩溃。添加单个 TextView 时效果很好,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51196176/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com