无法在Android中使用警报对话框打开微调器

时间:2016-03-17 19:58:02

标签: android android-spinner android-alertdialog

我正在开发一款Android应用。在我的应用程序中,我想在单击按钮时打开带有对话框的微调器。我在线搜索代码。我试过了。但这是错误。

这是带有打开微调器对话框的按钮的活动

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

我该如何解决?

2 个答案:

答案 0 :(得分:3)

替换

 AlertDialog.Builder b = new AlertDialog.Builder(getBaseContext());

by -

  AlertDialog.Builder b = new AlertDialog.Builder(MainActivity.this); //Or whatever activity name is

答案 1 :(得分:0)

将getBaseContext()更改为您的class.this 像
     AlertDialog.Builder b = new AlertDialog.Builder(YourActivityClass.this);