0

当我单击此警报对话框的肯定按钮时,应用程序崩溃。这段代码有什么问题吗?我感觉复选框分配不正确。

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)
4

2 回答 2

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);
于 2013-06-14T20:44:14.257 回答
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>
于 2013-06-14T21:17:18.733 回答