单击AlertDialog中的PositiveButton时崩溃[Android]

时间:2013-06-14 20:42:26

标签: android android-alertdialog android-checkbox

当我点击此警告对话框的正面按钮时,应用程序崩溃了。这段代码有什么问题吗?我感觉复选框没有正确分配。

private void showSmimeSettings(){

    AlertDialog.Builder smimeBuilder = new AlertDialog.Builder(this);
    smimeBuilder.setTitle("S/MIME Settings");

    LayoutInflater inflator = getLayoutInflater();
    View checkboxLayout = inflator.inflate(R.layout.smime_settings_checkbox, null);
    smimeBuilder.setView(checkboxLayout);

    final CheckBox signed_checkbox = (CheckBox) findViewById(R.id.signed_checkBox);
    final CheckBox encrypted_checkbox = (CheckBox) findViewById(R.id.encrypted_checkBox);


    smimeBuilder.setPositiveButton("Save", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface arg0, int arg1) {
            if (signed_checkbox.isChecked()) {
                mIsSigned = true;
            } else {
                mIsSigned = false;
            }
            if (encrypted_checkbox.isChecked()) {
                mIsEncrypted = true;
            } else {
                mIsEncrypted = false;
            }
        }
    });

    AlertDialog smimeDialog = smimeBuilder.create();
    smimeDialog.show();     
}

日志:

    06-14 17:08:59.041: E/com.fm.sg.android.sgApplication(4131): Thread uncaught exception:
06-14 17:08:59.041: E/com.fm.sg.android.sgApplication(4131): java.lang.NullPointerException
06-14 17:08:59.041: E/com.fm.sg.android.sgApplication(4131):    at com.android.lexperts.email.activity.MessageCompose$7.onClick(MessageCompose.java:1647)
06-14 17:08:59.041: E/com.fm.sg.android.sgApplication(4131):    at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
06-14 17:08:59.041: E/com.fm.sg.android.sgApplication(4131):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-14 17:08:59.041: E/com.fm.sg.android.sgApplication(4131):    at android.os.Looper.loop(Looper.java:137)
06-14 17:08:59.041: E/com.fm.sg.android.sgApplication(4131):    at android.app.ActivityThread.main(ActivityThread.java:5039)
06-14 17:08:59.041: E/com.fm.sg.android.sgApplication(4131):    at java.lang.reflect.Method.invokeNative(Native Method)
06-14 17:08:59.041: E/com.fm.sg.android.sgApplication(4131):    at java.lang.reflect.Method.invoke(Method.java:511)
06-14 17:08:59.041: E/com.fm.sg.android.sgApplication(4131):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-14 17:08:59.041: E/com.fm.sg.android.sgApplication(4131):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-14 17:08:59.041: E/com.fm.sg.android.sgApplication(4131):    at dalvik.system.NativeStart.main(Native Method)
06-14 17:08:59.041: E/ACRA(4131): ACRA caught a NullPointerException exception for com.fm.sg.android. Building report.
06-14 17:09:02.252: E/ACRA(4131): com.fm.sg.android fatal error : null
06-14 17:09:02.252: E/ACRA(4131): java.lang.NullPointerException
06-14 17:09:02.252: E/ACRA(4131):   at com.android.lexperts.email.activity.MessageCompose$7.onClick(MessageCompose.java:1647)
06-14 17:09:02.252: E/ACRA(4131):   at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
06-14 17:09:02.252: E/ACRA(4131):   at android.os.Handler.dispatchMessage(Handler.java:99)
06-14 17:09:02.252: E/ACRA(4131):   at android.os.Looper.loop(Looper.java:137)
06-14 17:09:02.252: E/ACRA(4131):   at android.app.ActivityThread.main(ActivityThread.java:5039)
06-14 17:09:02.252: E/ACRA(4131):   at java.lang.reflect.Method.invokeNative(Native Method)
06-14 17:09:02.252: E/ACRA(4131):   at java.lang.reflect.Method.invoke(Method.java:511)
06-14 17:09:02.252: E/ACRA(4131):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-14 17:09:02.252: E/ACRA(4131):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-14 17:09:02.252: E/ACRA(4131):   at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:1)

我认为你应该在给布局膨胀后检索你的复选框。

LayoutInflater inflator = getLayoutInflater();
View checkboxLayout = inflator.inflate(R.layout.smime_settings_checkbox, null);

final CheckBox signed_checkbox = (CheckBox) checkboxLayout.findViewById(R.id.signed_checkBox);
final CheckBox encrypted_checkbox = (CheckBox) checkBoxLayout.findViewById(R.id.encrypted_checkBox);

答案 1 :(得分:1)

private void showSmimeSettings(){

    AlertDialog.Builder smimeBuilder = new AlertDialog.Builder(this);
    smimeBuilder.setTitle("S/MIME Settings");

    LayoutInflater inflator = getLayoutInflater();
    View checkboxLayout = inflator.inflate(R.layout.smime_settings_checkbox, null);

    smimeBuilder.setView(checkboxLayout);
    final CheckBox signed_checkbox = (CheckBox) checkboxLayout.findViewById(R.id.signed_checkBox);
    final CheckBox encrypted_checkbox = (CheckBox) checkboxLayout.findViewById(R.id.encrypted_checkBox);


    smimeBuilder.setPositiveButton("Save", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface arg0, int arg1) {
            if (signed_checkbox.isChecked()) {
                mIsSigned = true;
            } else {
                mIsSigned = false;
            }
            if (encrypted_checkbox.isChecked()) {
                mIsEncrypted = true;
            } else {
                mIsEncrypted = false;
            }
        }
    });

    AlertDialog smimeDialog = smimeBuilder.create();
    smimeDialog.show();     
}

布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <CheckBox
        android:id="@+id/signed_checkBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="CheckBox" 
        />

    <CheckBox
        android:id="@+id/encrypted_checkBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="CheckBox" />

</LinearLayout>