I’ve got button in my Activity and I want to show AlertDialog when button is clicked:
@Override
public void onClick(View view) {
case R.id.btnDetailedCall:
final String[] phoneArray=ad.getPhone().split(" ");
if(phoneArray.length>1){
AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder.setTitle("Title");
builder.setSingleChoiceItems(phoneArray, -1, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
selectedPhone=phoneArray[which];
}
});
AlertDialog dialog=builder.create();
dialog.show();
}
When I pass “this” to AlertDialog constructor code runs normally but dialog does not appear on the screen. I believe that “this” is not the right reference here, so I tried getBaseContext() and got WindowManager$BadTockenException: Unable to add window -- tocken null is not for an application
Will appreciate any help, thanks.
Passing “this” may reference the wrong object, trying passing “ClassName.this” where ClassName is the name of the actual class that you’re using – here is a sample block of code that I use when requesting for user text input in an application that may be of help: