在运行时点击添加按钮?

时间:2013-04-13 22:18:31

标签: java android

public class MainActivity extends Activity implements View.OnClickListener {
    int i, j, butNum, lay1num = 1, lay2num = 100, lay3num = 100, store;
    Button[] Button;
    EditText numBut;
    LinearLayout mainLayout;
    LinearLayout[] subLayout;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        butNum = 5;
        Button = new Button[butNum];
        subLayout = new LinearLayout[3];
        LinearLayout mainLayout = new LinearLayout(this);
        mainLayout.setOrientation(LinearLayout.HORIZONTAL);
        mainLayout.setWeightSum(90);
        mainLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.MATCH_PARENT));

        subLayout[0] = new LinearLayout(this);
        subLayout[0].setOrientation(LinearLayout.VERTICAL);
        subLayout[0].setLayoutParams(new LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 30));
        subLayout[0].setGravity(Gravity.CENTER_HORIZONTAL);
        subLayout[1] = new LinearLayout(this);
        subLayout[1].setOrientation(LinearLayout.VERTICAL);
        subLayout[1].setLayoutParams(new LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 30));
        subLayout[2] = new LinearLayout(this);
        subLayout[2].setOrientation(LinearLayout.VERTICAL);
        subLayout[2].setLayoutParams(new LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 30));
        subLayout[0].setGravity(0x10);
        for (i = 0; i < 3; i++) {
            for (j = 0; j < butNum; j++) {
                int value = j + 1;
                Button[j] = new Button(this);
                Button[j].setText("" + value);
                Button[j].setLayoutParams(new LayoutParams(
                        LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
                Button[j].setTextSize(20);
                Button[j].setWidth(100 + j * 20);
                Button[j].setTag(value);
                Button[j].setId(j);
                subLayout[i].addView(Button[j]);
                Button[j].setOnClickListener(this);
            }
        }

        TextView text = new TextView(this);
        text.setText("" + store);
        subLayout[0].addView(text);
        mainLayout.addView(subLayout[0]);
        mainLayout.addView(subLayout[1]);
        mainLayout.addView(subLayout[2]);

        setContentView(mainLayout);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    public void onClick(View v) {
        // TODO Auto-generated method stub
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        switch (v.getId()) {
        case 0:
            subLayout[0].addView(Button[0], layoutParams);
            break;
        }

    }

}

我使用java创建了三个布局并将它们添加到mainlayout。活动开始时,我在第一个布局中添加了按钮。我想要做的是在点击按钮时向subLayout [1]添加一个新按钮。但是当我运行程序时,当我点击按钮时崩溃(说程序意外停止).LogCat是:

 04-14 03:52:39.174: D/AndroidRuntime(349): Shutting down VM
04-14 03:52:39.174: W/dalvikvm(349): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
04-14 03:52:39.184: E/AndroidRuntime(349): FATAL EXCEPTION: main
04-14 03:52:39.184: E/AndroidRuntime(349): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
04-14 03:52:39.184: E/AndroidRuntime(349):  at android.view.ViewGroup.addViewInner(ViewGroup.java:1970)
04-14 03:52:39.184: E/AndroidRuntime(349):  at android.view.ViewGroup.addView(ViewGroup.java:1865)
04-14 03:52:39.184: E/AndroidRuntime(349):  at android.view.ViewGroup.addView(ViewGroup.java:1845)
04-14 03:52:39.184: E/AndroidRuntime(349):  at com.creos.towerofhanoi.MainActivity.onClick(MainActivity.java:85)
04-14 03:52:39.184: E/AndroidRuntime(349):  at android.view.View.performClick(View.java:2408)
04-14 03:52:39.184: E/AndroidRuntime(349):  at android.view.View$PerformClick.run(View.java:8816)
04-14 03:52:39.184: E/AndroidRuntime(349):  at android.os.Handler.handleCallback(Handler.java:587)
04-14 03:52:39.184: E/AndroidRuntime(349):  at android.os.Handler.dispatchMessage(Handler.java:92)
04-14 03:52:39.184: E/AndroidRuntime(349):  at android.os.Looper.loop(Looper.java:123)
04-14 03:52:39.184: E/AndroidRuntime(349):  at android.app.ActivityThread.main(ActivityThread.java:4627)
04-14 03:52:39.184: E/AndroidRuntime(349):  at java.lang.reflect.Method.invokeNative(Native Method)
04-14 03:52:39.184: E/AndroidRuntime(349):  at java.lang.reflect.Method.invoke(Method.java:521)
04-14 03:52:39.184: E/AndroidRuntime(349):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-14 03:52:39.184: E/AndroidRuntime(349):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-14 03:52:39.184: E/AndroidRuntime(349):  at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

我终于解决了它必须在onclick中指定按钮参数。这是一个小错误

public void onClick(View v) {
        // TODO Auto-generated method stub

        switch (v.getId()) {
        case 0:

            Button[0] = new Button(this);
            Button[0].setLayoutParams(new LayoutParams(
                    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
            subLayout[1].addView(Button[0]);


            break;
        }

    }