0

我需要每个复选框都有自己的值,例如 1、2 或 3,并且您只能检查前 3 个复选框中的 1 个(cb1、cb2、cb3),并且只能检查后 3 个复选框中的 1 个(cb4、cb5、cb6)。但最重要的是获得检查哪些复选框的摘要。应用程序崩溃,我做错了什么?ps 请原谅我的英语不好这是完整的源代码:

public class MainActivity extends Activity {
    LinearLayout layout;
    Button bt;
    CheckBox cb1, cb2, cb3, cb4, cb5, cb6;
    TextView tv;
    String a, b, c;
    int d, e, f, g, h, i;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        layout = new LinearLayout(this);
        layout.setOrientation(LinearLayout.VERTICAL);
        cb1 = new CheckBox(this);
        cb2 = new CheckBox(this);
        cb3 = new CheckBox(this);
        cb4 = new CheckBox(this);
        cb5 = new CheckBox(this);
        cb6 = new CheckBox(this);
        bt = new Button(this);
        tv = new TextView(this);
        bt.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                a = "";
                b = "";
                c = "";
                d = 1;
                e = 2;
                f = 3;
                if (cb1.isChecked()){
                    a = Integer.toString(d);
                }else if (cb2.isChecked()){
                    a = Integer.toString(e);
                }else if (cb3.isChecked()){
                    a = Integer.toString(f);
                }else if (cb4.isChecked()){
                    b = Integer.toString(d);
                }else if (cb5.isChecked()){
                    b = Integer.toString(e);
                }else if (cb6.isChecked()){
                    b = Integer.toString(f);
                }
                g = Integer.valueOf(a);
                h = Integer.valueOf(b);
                i = g+h;
                c = Integer.toString(i);
                tv.setText(c);
            }
        });
        layout.addView(cb1);
        layout.addView(cb2);
        layout.addView(cb3);
        layout.addView(cb4);
        layout.addView(cb5);
        layout.addView(cb6);
        layout.addView(bt);
        layout.addView(tv);
        setContentView(layout);

    }
}

10-20 00:13:15.580: W/dalvikvm(2826): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
10-20 00:13:15.700: E/AndroidRuntime(2826): FATAL EXCEPTION: main
10-20 00:13:15.700: E/AndroidRuntime(2826): java.lang.NullPointerException
10-20 00:13:15.700: E/AndroidRuntime(2826):     at com.example.test.MainActivity$1.onClick(MainActivity.java:58)
10-20 00:13:15.700: E/AndroidRuntime(2826):     at android.view.View.performClick(View.java:4204)
10-20 00:13:15.700: E/AndroidRuntime(2826):     at android.view.View$PerformClick.run(View.java:17355)
10-20 00:13:15.700: E/AndroidRuntime(2826):     at android.os.Handler.handleCallback(Handler.java:725)
10-20 00:13:15.700: E/AndroidRuntime(2826):     at android.os.Handler.dispatchMessage(Handler.java:92)
10-20 00:13:15.700: E/AndroidRuntime(2826):     at android.os.Looper.loop(Looper.java:137)
10-20 00:13:15.700: E/AndroidRuntime(2826):     at android.app.ActivityThread.main(ActivityThread.java:5041)
10-20 00:13:15.700: E/AndroidRuntime(2826):     at java.lang.reflect.Method.invokeNative(Native Method)
10-20 00:13:15.700: E/AndroidRuntime(2826):     at java.lang.reflect.Method.invoke(Method.java:511)
10-20 00:13:15.700: E/AndroidRuntime(2826):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
10-20 00:13:15.700: E/AndroidRuntime(2826):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
10-20 00:13:15.700: E/AndroidRuntime(2826):     at dalvik.system.NativeStart.main(Native Method)
4

2 回答 2

2

这些行:

g = Integer.valueOf(a);
h = Integer.valueOf(b);

会给你一个错误,因为要么aorb将永远等于""并且你不能Integer.valueOf(String)在空的String.

要解决此问题,请使用以下代码而不是这些行:

if (!a.equals(""))
    g = Integer.valueOf(a);
else
    g = 0;

if (!b.equals(""))
    h = Integer.valueOf(b);
else
    h = 0;
于 2013-10-20T00:00:13.300 回答
1

如果您的 CheckBoxes 都没有被选中,a并且b""(一个空字符串)。所以g = Integer.valueOf(a);而且h = Integer.valueOf(b);必须失败。

于 2013-10-19T23:59:52.143 回答