1

I am trying to implement an AlertDialogBox upon a button click. But I don't know for what reason the AlertDialog box wont start. Here what I have coded up till now :

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();
            }
        });

I debugged the code and it is giving me this error :

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$1.onClick(IciciBank.java:86)

Any ideas ?

TechFrk
  • 185
  • 2
  • 2
  • 15

1 Answers1

3

Your problem is using getApplicationContext() as context. AlertDialog.Builder just needs activity context. Here is an good explanation about context.

Replace

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

With

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

Hope it will be useful for you.

Community
  • 1
  • 1
Arkar Aung
  • 3,554
  • 1
  • 16
  • 25