Warning dialog not displayed

Hi everyone, I am creating an alert dialog box in android by clicking a button. I used the onClick property for XML and the calling function. my code

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 does not show a warning and gives an error, for example

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

Please tell me what is wrong with this code.

+6
source share
1 answer
 new AlertDialog.Builder(getApplicationContext()); 

I think this is a problem. You tried:

 new AlertDialog.Builder(YourActivityClassName.this); 
+17
source

Source: https://habr.com/ru/post/900138/


All Articles