我正在开发 android 中的记事本应用程序。我有一个 SherlockListActivity,我用数据库中的信息填充它的 ListView。为了显示动作栏的动作模式,我放置了一个长按的监听器。直到这里,一切都很好。问题是当我单击删除选项时,它会显示一个 AlertDialog 询问我是否确定,当我单击是时,它应该从数据库中删除它,但是应用程序会冻结并关闭。我已经在这个网站上找到了 3 个答案,显然其他人也有这个问题,我尝试和他们一样,但没有成功。谁能告诉我应该如何进行?这是我的代码:
public class Scratchpad extends SherlockListActivity {
private static final int ACTIVITY_CREATE=0;
private static final int ACTIVITY_EDIT=1;
private static final int INSERT_ID = Menu.FIRST;
private static final int DELETE_ID = Menu.FIRST + 1;
private static final int EDIT_ID = Menu.FIRST + 2;
private ScratchHelper mDbHelper;
protected Object mActionMode;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.scratch_list);
mDbHelper = new ScratchHelper(this);
mDbHelper.open();
fillData(); //populates the ListView with info from my DB
registerForContextMenu(getListView());
getListView().setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
int position, long id) {
if (mActionMode != null) {
return false;
}
// Start the CAB using the ActionMode.Callback defined above
mActionMode = Scratchpad.this
.startActionMode(mActionModeCallback);
view.setSelected(true);
return true;
}
});
}
private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {
// Called when the action mode is created; startActionMode() was called
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
// Inflate a menu resource providing context menu items
MenuInflater inflater = mode.getMenuInflater();
// Assumes that you have "contexual.xml" menu resources
inflater.inflate(R.menu.actionmenu_main_longclick, menu);
return true;
}
// Called each time the action mode is shown. Always called after
// onCreateActionMode, but
// may be called multiple times if the mode is invalidated.
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false; // Return false if nothing is done
}
// Called when the user selects a contextual menu item
public boolean onActionItemClicked(ActionMode mode, final MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_long_delete:
AlertDialog.Builder alertDialog = new AlertDialog.Builder(Scratchpad.this);
// Setting Dialog Title
alertDialog.setTitle("Confirm Delete...");
// Setting Dialog Message
alertDialog.setMessage(R.string.confirm_delete);
// Setting Positive "Yes" Button
alertDialog.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
mDbHelper.deleteNote(info.id); //THERE IS THE PROBLEM!!!!!!!!
fillData();
}
});
// Setting Negative "NO" Button
alertDialog.setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
// Showing Alert Message
alertDialog.show();
// Action picked, so close the CAB
mode.finish();
return true;
case R.id.menu_long_edit:
Toast.makeText(getApplicationContext(), R.string.scratch_modified, Toast.LENGTH_SHORT).show();
// Action picked, so close the CAB
mode.finish();
return true;
default:
return false;
}
}
// Called when the user exits the action mode
public void onDestroyActionMode(ActionMode mode) {
mActionMode = null;
selectedItem = -1;
}
};
问题出在我评论“这是问题所在”的那一行。