Android AlertDialog box WindowManager$BadTokenExce

2019-02-16 16:37发布

问题:

I am using the the following code for a context menu and then if user chose delete, a dialog massage will appear.

infos.setOnCreateContextMenuListener(new OnCreateContextMenuListener(){
            //@Override
            public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
                menu.setHeaderTitle("Context Menu");
                menu.add(0, CONTEXT_EDIT, 0, "Edit Item");
                menu.add(0, CONTEXT_DELETE, 1, "Delete Item");
            }
});

public boolean onContextItemSelected(MenuItem item) {

        AdapterView.AdapterContextMenuInfo menuInfo = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
        final Long _id = menuInfo.id;
        //selected_row = menuInfo.position;

        // To get the id of the clicked item in the list use menuInfo.id
        switch (item.getItemId()) {
            case CONTEXT_EDIT:
                addEditRes(_id);
                break;
            case CONTEXT_DELETE:
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setMessage("Are you sure you want to delete?")
                       .setCancelable(false)
                       .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                           public void onClick(DialogInterface dialog, int id) {
                               infoDataHelper.deleteRes(_id);  
                               model = infoDataHelper.getCursor(addType);
                               adapter.changeCursor(model);
                           }
                       })
                       .setNegativeButton("No", new DialogInterface.OnClickListener() {
                           public void onClick(DialogInterface dialog, int id) {
                                dialog.cancel();
                           }
                       });
                AlertDialog alert = builder.create();   
                alert.show();
                break;
            default:
                return super.onContextItemSelected(item);

        }
        adapter.notifyDataSetChanged();
        return true;
}

But as soon i am choosing the delete, it is giving the following error.

android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application

What is the problem in my code?

回答1:

I believe the problem could be on this line:

AlertDialog.Builder builder = new AlertDialog.Builder(this);

Try modifying it to:

AlertDialog.Builder builder = new AlertDialog.Builder(MyActivityName.this);

Replacing MyActivityName with the name of your activity.

Did that fix the error?



回答2:

It should be AlertDialog.Builder builder = new AlertDialog.Builder(this.getParent());

Because the activity is in a tabactivity within another tabactivity.



回答3:

I was getting the same error. I changed

AlertDialog.Builder builder = new AlertDialog.Builder(this);

to

 AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);

Its working fine now. Thanks.



回答4:

Kotlin

Change from this:

val builder: AlertDialog.Builder = AlertDialog.Builder(this)

to

val builder: AlertDialog.Builder = AlertDialog.Builder(this@MainActivity)