私はアンドロイドでメモ帳アプリケーションに取り組んでいます。SherlockListActivity があり、ListView にデータベースからの情報を入力します。Actionbar の ActionMode を表示するために、ロングクリックのリスナーを配置しました。ここまでは、すべて順調です。問題は、削除オプションをクリックすると、確認するかどうかを尋ねる 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;
}
};
問題は、「HERE IS THE PROBLEM」とコメントした行にあります。