CAB無法刪除光標適配器數據語境操作欄無法從SQLite的
public class History extends ListActivity{
private ReminderDB mDbHelper;
List selections = new ArrayList();
int count = 0;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.history);
mDbHelper = new ReminderDB(this);
mDbHelper.open();
fillData();
registerForContextMenu(getListView());
}
private void fillData() {
final Cursor remindersCursor = mDbHelper.fetchAllHistoryEvents();
startManagingCursor(remindersCursor);
// an array for the fields to show in the list row (now only the TITLE)
String[] from = new String[]{ReminderDB.KEY_TITLE};
// and an array of the fields for each list row
int[] to = new int[]{R.id.row1};
// a simple cursor adapter and set it to display
final HistoryCustomCursorAdapter history = new HistoryCustomCursorAdapter(this, R.layout.history_list_row, remindersCursor, from, to);
setListAdapter(history);
getListView().setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE_MODAL);
getListView().setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
@Override
public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
if(checked){
selections.add(history.getItem(position));
count++;
mode.setTitle(count+ " Selected");
}else{
selections.remove(history.getItem(position));
count--;
mode.setTitle(count+ " Selected");
}
}
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.contextual_menu, menu);
return true;
}
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
if (item.getItemId()==R.id.item_delete){
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
mDbHelper.deleteHistory(info.id);
history.notifyDataSetChanged();
mode.finish();
}
fillData();
return true;
}
@Override
public void onDestroyActionMode(ActionMode mode) {
count = 0;
selections.clear();
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
fillData();
}
}
這是錯誤消息我得到:
11-08 14/24:03.475 6934-6934/com.example.windchin.digitalbrain E/AndroidRuntime:致命例外:main java.lang.NullPointerException at com.example.windchin.d igitalbrain.History $ 1.onActionItemClicked(History.java:83) at android.widget.AbsListView $ MultiChoiceModeWrapper.onActionItemClicked(AbsListView.java:7680) at com.android.internal.policy.impl.PhoneWindow $ DecorView $ ActionModeCallbackWrapper.onActionItemClicked (PhoneWindow.java:3299) at com.android.internal.app.ActionBarImpl $ ActionModeImpl.onMenuItemSelected(ActionBarImpl.java:1009) at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735 ) 在com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152) 在com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874) 在com.android .internal.view.menu.ActionMenuView.invokeItem(ActionMenuView.java:630) (android.view.java:4475) at android.view.View $ PerformClick.run(View .java:18786) at android.os.Handler.handleCallback(Handler.java:730) at android.os.Handler.dispatchMessage(Handler.java:92) at android.os.Looper.loop(Looper.java :Method.java:176) at android.app.ActivityThread.main(ActivityThread.java:5419) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java: 525) at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1046) at com.android.internal.os.ZygoteInit.main(Zy goteInit.java:862) 在dalvik.system.NativeStart.main(本機方法)
這是光標適配器類
public class HistoryCustomCursorAdapter extends SimpleCursorAdapter {
private Context context;
private ReminderDB mDbHelper;
private int layout;
public HistoryCustomCursorAdapter (Context context, int layout, Cursor c, String[] from, int[] to) {
super(context, layout, c, from, to);
this.context = context;
this.layout = layout;
mDbHelper = new ReminderDB(context);
mDbHelper.open();
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
final LayoutInflater inflater = LayoutInflater.from(context);
View rowView = inflater.inflate(layout, parent, false);
int titleCol = cursor.getColumnIndex(ReminderDB.KEY_TITLE);
String title = cursor.getString(titleCol);
TextView textView1 = (TextView) rowView.findViewById(R.id.row1);
TextView textView2 = (TextView) rowView.findViewById(R.id.row2);
ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
textView1.setText(title);
String Hdate = cursor.getString(cursor.getColumnIndex(ReminderDB.KEY_DATE_TIME));
String latitude = cursor.getString(cursor.getColumnIndex(ReminderDB.KEY_LATITUDE));
String longitude = cursor.getString(cursor.getColumnIndex(ReminderDB.KEY_LONGITUDE));
if(Hdate != null)
imageView.setImageResource(R.drawable.cclock);
else
imageView.setImageResource(R.drawable.maps);
if(Hdate != null)
{
textView2.setText(Hdate);
}
else
{
Cursor c = mDbHelper.fetchLocation(latitude, longitude);
String location = c.getString(c.getColumnIndex(ReminderDB.KEY_LOCATION));
int option = Integer.parseInt(cursor.getString(cursor.getColumnIndex(ReminderDB.KEY_LOCATIONOPTION)));
String locationOption;
if (option == 0){
locationOption = "When I Arrive";
} else {
locationOption = "When I Leave";
}
textView2.setText(locationOption + ": " + location);
}
return rowView;
}
}
哪行是您的歷史對象中的第83行? – Jim
@Jim mDbHelper.deleteHistory(info.id); – Wind