无法在 android 中启动 AlertDialog 框

Unable to start AlertDialog box in android

我试图在单击按钮时实现 AlertDialogBox。但我不知道 AlertDialog 框不会启动的原因。这是我到目前为止编写的代码:

netbalrefreshbtn = (Button) findViewById(R.id.netbalrefreshbtn);
        netbalrefreshbtn.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v) 
            {
                // TODO Auto-generated method stub
                AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getApplicationContext());
                alertDialogBuilder.setMessage(" Update Values")
                                  .setPositiveButton("Ok", new DialogInterface.OnClickListener() 
                                  {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) 
                                    {
                                        // TODO Auto-generated method stub
//                                      if(selecteditem == "Missed Call")
//                                      {
                                            String url = "tel:123456";
                                            Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
                                            startActivity(intent);
//                                      }

                                    }
                                })
                                .setNegativeButton("Cancel", new DialogInterface.OnClickListener() 
                                {   
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) 
                                    {
                                        // TODO Auto-generated method stub
                                        dialog.dismiss();
                                    }
                                });
                AlertDialog alertDialog = alertDialogBuilder.create();
                alertDialog.show();
            }
        });

我调试了代码,它给我这个错误:

03-23 13:59:36.450: E/AndroidRuntime(16221): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?
03-23 13:59:36.450: E/AndroidRuntime(16221):    at android.view.ViewRootImpl.setView(ViewRootImpl.java:562)
03-23 13:59:36.450: E/AndroidRuntime(16221):    at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:282)
03-23 13:59:36.450: E/AndroidRuntime(16221):    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:85)
03-23 13:59:36.450: E/AndroidRuntime(16221):    at android.app.Dialog.show(Dialog.java:298)
03-23 13:59:36.450: E/AndroidRuntime(16221):    at com.techfrk.fetchinboxsms.IciciBank.onClick(IciciBank.java:86)

有什么想法吗?

您的问题是将 getApplicationContext() 用作 context. AlertDialog.Builder just needs activity context. Here is an good explanation about context

替换

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

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(YourActivity.this);

希望对你有用。