0

ボタン 1 から 9 を作成し、それをループで実行したい。しかし、3 カウントごとに、新しい LinearLayout を作成したいと考えています。

  final LinearLayout[] ll2 = new LinearLayout[10]; // create an empty array;


            for(int i=1; i<=9;i++)
            {
                Button btnNums = new Button(this);
            final LinearLayout[] ll2 = new LinearLayout[10]; // create an empty array;


            for(int i=1; i<=9;i++)
            {
                Button btnNums = new Button(this);
                btnNums.setText(i+"");
                ll.addView(btnNums);
                if(i%3==0){
                    ll2[i] = ll;
                    ll = null;

                }
            }

            layout.addView(ll2[0]);

    btnNums.setText(i+"");
            ll.addView(btnNums);
            if(i%3==0){
                ll2[i] = ll;
                ll = null;

            }
        }

        layout.addView(ll2[0]);

これは動作しません。エラーは発生しませんが、アプリを実行すると動作が停止します。どうしたの?

4

1 に答える 1

1

私は自分のプロジェクトでそれを使用しましたが、うまくいきました。このように使用しました。それが役に立てば幸い

クラス レベルで空の配列を宣言します。

LinearLayout[] imageLayoutContainers = new LinearLayout[10];

そして onCreate method で:

 for (int i = 0; i < imageLayoutContainers.length; i++) {
        imageLayoutContainers[i] = new LinearLayout(this);
        imageLayoutContainers[i].setOrientation(LinearLayout.VERTICAL);
        imageLayoutContainers[i].setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        imageLayoutContainers[i].setBackgroundResource(imagesIds[i]);
    }

うまくいきました、ありがとう

于 2014-07-21T07:29:01.090 に答える