Android - 单击后使自定义警报对话消失

时间:2013-06-18 22:13:16

标签: java android listener android-spinner android-alertdialog

我试图这样做,以便当按下按钮时,警告对话框中会弹出一个微调器,并且在微调器中有三个选项,我怎样才能使它在选择其中一个选项时alertdialogue消失了,每当我从微调器

中选择一些东西时,我的代码就会崩溃

以下是我的代码

 public void AlertD() {

    LayoutInflater li = LayoutInflater.from(getActivity());

    View promptsView = li.inflate(R.layout.abs, null);
    // above I am setting the customview of the alert dialogue

    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
            getActivity());

    alertDialogBuilder.setView(promptsView);
    // here I am officially setting the custom layout

    // set dialog message

    alertDialogBuilder.setTitle(dialoguechoice + " Stretch Type");
    // alert dialogue title

    // create alert dialog
    final AlertDialog alertDialog = alertDialogBuilder.create();
    // above is creating the alert dialogue

    final Spinner mSpinner = (Spinner) promptsView
            .findViewById(R.id.sSType);
    // above is initializing the spinner

    // reference UI elements from my_dialog_layout in similar fashion

    mSpinner.setOnItemSelectedListener(new OnSpinnerItemClicked());

    // show it
    alertDialog.show();
    alertDialog.setCanceledOnTouchOutside(true);

} 

以下是我的听众

public class OnSpinnerItemClicked implements OnItemSelectedListener {
    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int pos,
            long id) {
        switch (pos) {
        case (1):

            alertDialog.dismiss();
            break;
        }
    }


    @Override
    public void onNothingSelected(AdapterView parent) {
        // Do nothing.
    }
}

EDIT logcat低于

06-18 18:09:55.939: E/AndroidRuntime(4355): FATAL EXCEPTION: main
06-18 18:09:55.939: E/AndroidRuntime(4355): java.lang.NullPointerException
06-18 18:09:55.939: E/AndroidRuntime(4355):     at com.OptimusApps.stayhealthy.Body$OnSpinnerItemClicked.onItemSelected(Body.java:178)
06-18 18:09:55.939: E/AndroidRuntime(4355):     at android.widget.AdapterView.fireOnSelected(AdapterView.java:882)
06-18 18:09:55.939: E/AndroidRuntime(4355):     at android.widget.AdapterView.access$200(AdapterView.java:48)
06-18 18:09:55.939: E/AndroidRuntime(4355):     at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:848)
06-18 18:09:55.939: E/AndroidRuntime(4355):     at android.os.Handler.handleCallback(Handler.java:605)
06-18 18:09:55.939: E/AndroidRuntime(4355):     at android.os.Handler.dispatchMessage(Handler.java:92)
06-18 18:09:55.939: E/AndroidRuntime(4355):     at android.os.Looper.loop(Looper.java:137)
06-18 18:09:55.939: E/AndroidRuntime(4355):     at android.app.ActivityThread.main(ActivityThread.java:4575)
06-18 18:09:55.939: E/AndroidRuntime(4355):     at java.lang.reflect.Method.invokeNative(Native Method)
06-18 18:09:55.939: E/AndroidRuntime(4355):     at java.lang.reflect.Method.invoke(Method.java:511)
06-18 18:09:55.939: E/AndroidRuntime(4355):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
06-18 18:09:55.939: E/AndroidRuntime(4355):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
06-18 18:09:55.939: E/AndroidRuntime(4355):     at dalvik.system.NativeStart.main(Native Method)

由于

0 个答案:

没有答案