我正在尝试做到这一点,以便当按下按钮时会弹出一个带有微调器的 alertdialogue,并且在微调器中有三个选项,我怎样才能做到这一点,以便在选择其中一个选项时 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.
}
}
编辑 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)
谢谢