复选框自动选中按钮单击

时间:2013-08-22 08:37:29

标签: java android xml android-layout

我想点击按钮时选中一个复选框。当我运行此代码时,它会给出空指针异常 这是我的代码

CheckBox  mCheckBoxRegular = (CheckBox) findViewById(R.id.checkBoxRegular);
Button regular = (Button)findViewById(R.id.buttonRegular);

    regular.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {

                        mCheckBoxRegular.setChecked(true);

                    }


                }

例外是

08-22 13:24:14.947: E/AndroidRuntime(31390): FATAL EXCEPTION: main
08-22 13:24:14.947: E/AndroidRuntime(31390): java.lang.NullPointerException
08-22 13:24:14.947: E/AndroidRuntime(31390):    at com.example.amazingnumbersample.MainActivity$2$2.onClick(MainActivity.java:128)
08-22 13:24:14.947: E/AndroidRuntime(31390):    at android.view.View.performClick(View.java:3534)
08-22 13:24:14.947: E/AndroidRuntime(31390):    at android.view.View$PerformClick.run(View.java:14263)
08-22 13:24:14.947: E/AndroidRuntime(31390):    at android.os.Handler.handleCallback(Handler.java:605)
08-22 13:24:14.947: E/AndroidRuntime(31390):    at android.os.Handler.dispatchMessage(Handler.java:92)
08-22 13:24:14.947: E/AndroidRuntime(31390):    at android.os.Looper.loop(Looper.java:137)
08-22 13:24:14.947: E/AndroidRuntime(31390):    at android.app.ActivityThread.main(ActivityThread.java:4441)
08-22 13:24:14.947: E/AndroidRuntime(31390):    at java.lang.reflect.Method.invokeNative(Native Method)
08-22 13:24:14.947: E/AndroidRuntime(31390):    at java.lang.reflect.Method.invoke(Method.java:511)
08-22 13:24:14.947: E/AndroidRuntime(31390):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
08-22 13:24:14.947: E/AndroidRuntime(31390):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
08-22 13:24:14.947: E/AndroidRuntime(31390):    at dalvik.system.NativeStart.main(Native Method)

请帮帮我。

2 个答案:

答案 0 :(得分:2)

我已经尝试过您的代码并进行了一些更改。它看起来像你想要的那样工作。

public class MainActivity extends Activity {

    CheckBox mCheckBoxRegular;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mCheckBoxRegular = (CheckBox) findViewById(R.id.checkBox1);

        Button regular = (Button) findViewById(R.id.button1);

        regular.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                mCheckBoxRegular.setChecked(true);

            }
        });

    }

}

答案 1 :(得分:1)

试试这个..!

在你的setContentView(R.layout.activity)之后;

final CheckBox  mCheckBoxRegular = (CheckBox) findViewById(R.id.checkBoxRegular);
    Button regular = (Button)findViewById(R.id.buttonRegular);

        regular.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {

                            mCheckBoxRegular.setChecked(true);

                        }


                    });