Search code examples
androiddialogalert

Alert dialog is not displaying


Hi all I am creating an alert dialog in android by clicking a button. I used onClick property of XML and calling function. My code is

public void selectPhoneType(View view)
{
    String [] item = {"Home", "Work", "Mobile", "Work Fax", "Home Fax", "Pager", "Other", "Custom"};
    AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());
    builder.setTitle("Select Label");
    AlertDialog alert = builder.create();
    alert.show();}

but this code is not showing alert and giving error like

BadTokenException: Unable to add window -- token null is not for an application. 

Please tell me what is wrong with this code.


Solution

  • new AlertDialog.Builder(getApplicationContext());
    

    I think this is the problem. Have you tried:

    new AlertDialog.Builder(YourActivityClassName.this);