dev*_*oid 10 android window android-alertdialog
我使用以下代码作为上下文菜单,然后如果用户选择删除,将出现对话按摩.
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;
}
Run Code Online (Sandbox Code Playgroud)
但是,一旦我选择删除,它就会出现以下错误.
android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
Run Code Online (Sandbox Code Playgroud)
我的代码有什么问题?
Joa*_*und 29
我相信问题可能就在这条线上:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
Run Code Online (Sandbox Code Playgroud)
尝试将其修改为:
AlertDialog.Builder builder = new AlertDialog.Builder(MyActivityName.this);
Run Code Online (Sandbox Code Playgroud)
用您的活动名称替换MyActivityName.
这样可以解决错误吗?
它应该是AlertDialog.Builder builder = new AlertDialog.Builder(this.getParent());
因为活动是在另一个tabactivity内的tabactivity.
小智 6
我得到了同样的错误.我变了
AlertDialog.Builder builder = new AlertDialog.Builder(this);
Run Code Online (Sandbox Code Playgroud)
至
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
Run Code Online (Sandbox Code Playgroud)
它的工作正常.谢谢.
归档时间: |
|
查看次数: |
11847 次 |
最近记录: |