ループを介して複数のテキストビューを動的に追加しようとすると、アプリがクラッシュします。単一のテキストビューを追加する場合は正常に機能します

ユスフ

以下のコードを使用して複数のテキストビューを動的に追加しようとすると、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:致命的な例外:メインプロセス: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)at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3302)at android.app.ActivityThread.-wrap12(Unknown Source:0)atandroid.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)atcom。android.internal.os.Zygote $ MethodAndArgsCaller.run(Zygote.java:245)at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)原因:java.lang.IllegalStateException:指定された子はすでに親がいます。最初に子の親でremoveView()を呼び出す必要があります。android.view.ViewGroup.addViewInner(ViewGroup.java:4976)at android.view.ViewGroup.addView(ViewGroup.java:4807)at android.view.ViewGroup.addView(ViewGroup.java:4747)atandroid.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)atandroid.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:シグナルを送信しています。PID:12226 SIG:9アプリケーションが終了しました。ActivityThread.main(ActivityThread.java:7425)at java.lang.reflect.Method.invoke(Native Method)at com.android.internal.os.Zygote $ MethodAndArgsCaller.run(Zygote.java:245)atcom.android。 internal.os.ZygoteInit.main(ZygoteInit.java:921)I / Process:シグナルを送信しています。PID:12226 SIG:9アプリケーションが終了しました。ActivityThread.main(ActivityThread.java:7425)at java.lang.reflect.Method.invoke(Native Method)at com.android.internal.os.Zygote $ MethodAndArgsCaller.run(Zygote.java:245)atcom.android。 internal.os.ZygoteInit.main(ZygoteInit.java:921)I / Process:シグナルを送信しています。PID:12226 SIG:9アプリケーションが終了しました。

アブドゥルマレク・デリー

正しいコードは次のとおりです

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

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

Related 関連記事

ホットタグ

アーカイブ