无法在Android中打开带有警报对话框的微调器
我正在开发一个Android应用程序。在我的应用程序中,我想在单击按钮时打开一个带有对话框的微调器。我在网上搜索代码。我试过了。但这是一个错误 这是打开微调器对话框的带有按钮的活动无法在Android中打开带有警报对话框的微调器,android,android-spinner,android-alertdialog,Android,Android Spinner,Android Alertdialog,我正在开发一个Android应用程序。在我的应用程序中,我想在单击按钮时打开一个带有对话框的微调器。我在网上搜索代码。我试过了。但这是一个错误 这是打开微调器对话框的带有按钮的活动 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.create_item);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.create_item);
btnOpenCategorySpinner = (Button) findViewById(R.id.btn_open_category_spinnter);
btnOpenCategorySpinner.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder b = new AlertDialog.Builder(getBaseContext());
b.setTitle("Example");
String[] types = {"By Zip", "By Category"};
b.setItems(types, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
switch(which){
case 0:
break;
case 1:
break;
}
}
});
b.show();
}
});
}
这是我单击打开按钮时logcat中的错误
7 19:53:28.629 29116-29116/com.blog.waiyanhein.mmfashion.mmfashion W/EGL_genymotion: eglSurfaceAttrib not implemented
03-17 19:53:29.581 29116-29116/com.blog.waiyanhein.mmfashion.mmfashion D/AndroidRuntime: Shutting down VM
03-17 19:53:29.581 29116-29116/com.blog.waiyanhein.mmfashion.mmfashion W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0xa61c8908)
03-17 19:53:29.581 29116-29116/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: FATAL EXCEPTION: main
03-17 19:53:29.581 29116-29116/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
03-17 19:53:29.581 29116-29116/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: at android.view.ViewRootImpl.setView(ViewRootImpl.java:571)
03-17 19:53:29.581 29116-29116/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:246)
03-17 19:53:29.581 29116-29116/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
03-17 19:53:29.581 29116-29116/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: at android.app.Dialog.show(Dialog.java:281)
03-17 19:53:29.581 29116-29116/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: at android.app.AlertDialog$Builder.show(AlertDialog.java:951)
03-17 19:53:29.581 29116-29116/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: at com.blog.waiyanhein.mmfashion.mmfashion.CreateItemActivity$1.onClick(CreateItemActivity.java:58)
03-17 19:53:29.581 29116-29116/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: at android.view.View.performClick(View.java:4204)
03-17 19:53:29.581 29116-29116/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: at android.view.View$PerformClick.run(View.java:17355)
03-17 19:53:29.581 29116-29116/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:725)
03-17 19:53:29.581 29116-29116/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:92)
03-17 19:53:29.581 29116-29116/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: at android.os.Looper.loop(Looper.java:137)
03-17 19:53:29.581 29116-29116/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5041)
03-17 19:53:29.581 29116-29116/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
03-17 19:53:29.581 29116-29116/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:511)
03-17 19:53:29.581 29116-29116/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-17 19:53:29.581 29116-29116/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-17 19:53:29.581 29116-29116/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)
如何修复它?更换您的
AlertDialog.Builder b = new AlertDialog.Builder(getBaseContext());
藉—
替换你的
AlertDialog.Builder b = new AlertDialog.Builder(getBaseContext());
藉—
将getBaseContext()更改为类。此
像AlertDialog.Builder b=新建AlertDialog.Builder(YourActivityClass.this) 将getBaseContext()更改为您的类。此 像
AlertDialog.Builder b=新建AlertDialog.Builder(YourActivityClass.this) 非常感谢。成功了。我将对答案投赞成票。如何设置项的id并在click listener中检索它?您不需要设置id,在OnClick()中,您可以使用-types[which])获得与所选索引对应的所选文本,非常感谢。成功了。我将对答案投赞成票。如何设置项的id并在click listener中检索它?您不需要设置id,在OnClick()中,您可以使用-types[which]获得与所选索引对应的所选文本