Android的AlertDialog对话框窗口管理$ BadTokenException问题 [英] Android AlertDialog box WindowManager$BadTokenException problem

查看:366
本文介绍了Android的AlertDialog对话框窗口管理$ BadTokenException问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在用的是下面的$ C $下的上下文菜单,然后如果用户选择删除,一个对话框,按摩会出现。

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

有什么问题,我的code?

What is the problem in my code?

推荐答案

这应该是 AlertDialog.Builder建设者=新AlertDialog.Builder(this.getParent());

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

由于该活动是在另一个tabactivity一个tabactivity。

Because the activity is in a tabactivity within another tabactivity.

这篇关于Android的AlertDialog对话框窗口管理$ BadTokenException问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆