当我尝试通过以下代码动态添加多个文本视图时,Numbers活动崩溃 . 但是当我不使用循环但只添加文本视图时,它工作正常 .
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进程: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.A活动中的android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3194)android.app.A活动时,我发现它位于android.app.A活动时,活动强化(ActivityThread.java:3302) . ActivityThread $ H.handleMessage(ActivityThread.java:1891)位于android.app.AooT.Thread.main的android.os.Handler.dispatchMessage(Handler.java:108)android.os.Looper.loop(Looper.java:166) (ActivityThread.java:7425)位于com.android.internal.os的com.android.internal.os.Zygote $ MethodAndArgsCaller.run(Zygote.java:245)的java.lang.reflect.Method.invoke(Native Method) .ZygoteInit.main(ZygoteInit.java:921)引起:java.lang.IllegalStateException:指定的子节点已经有父节点 . 您必须首先在孩子的父母上调用removeView() . 在Android.view上的android.view.ViewGroup.addView(ViewGroup.java:4747)上的android.view.ViewGroup.addViewInner中的android.view.ViewGroup.addViewInner(ViewGroup.java:4976) . 位于android.app.Instrumentation的android.app.Activity.performCreate(Activity.java:7372)的com.example.yusuf.miwok.NumbersActivity.onCreate(NumbersActivity.java:40)中的ViewGroup.addView(ViewGroup.java:4720) .callActivityOnCreate(Instrumentation.java:1218)位于android.app.A活动中的android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3147)android.app.A活动 . 活动(活动传递:3:3),android.app.ActivityThread.-wrap12(未知)来源:0)在android.app.Loper.loop(Looper.java:166)的android.app.Handler.dispatchMessage(Handler.java:108)上的android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1891) )在android.app.ActivityThread.main(ActivityThread.java:7425)at java.lang.reflect.Method.invoke(Native Method)at com.android.internal.os.Zygote $ MethodAndArgsCaller.run(Zygote.jav) a:245)at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)I / Process:发送信号 . PID:12226 SIG:9应用程序终止 .
1 回答
正确的代码如下
请注意,我只是将此行移至for循环内部