Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 625
Lalit Chattar Avatar asked Oct 26 '11 14:10

Lalit Chattar


1 Answers

new AlertDialog.Builder(getApplicationContext());

I think this is the problem. Have you tried:

new AlertDialog.Builder(YourActivityClassName.this);
like image 173
NotACleverMan Avatar answered Nov 09 '22 08:11

NotACleverMan