CAB无法从游标适配器中删除数据
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:致命异常:主要 显示java.lang.NullPointerException at com.example.windchin.digitalbrain.History $ 1.onActionItemClicked(History.java:83) 在android.widget.AbsListView $ MultiChoiceModeWrapper.onActionItemClicked(AbsListView.java:7680) 在com.android.internal.policy.impl.PhoneWindow $ DecorView $ ActionModeCallbackWrapper.onActionItemClicked(PhoneWindow.java:3299) at com.android.internal.app.ActionBarImpl $ ActionModeImpl.onMenuItemSelected(ActionBarImpl.java:1009) 在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) 在com.android.internal.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:200) 在android.view.View.performClick(View.java:4475) 在android.view.View $ PerformClick.run(View.java:18786) 在android.os.Handler.handleCallback(Handler.java:730) 在android.os.Handler.dispatchMessage(Handler.java:92) 在android.os.Looper.loop(Looper.java:176) 在android.app.ActivityThread.main(ActivityThread.java:5419) at java.lang.reflect.Method.invokeNative(Native Method) 在java.lang.reflect.Method.invoke(Method.java:525) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1046) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.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;
}
}
答案 0 :(得分:0)
根据您的评论,您会遇到以下问题:
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
mDbHelper.deleteHistory(info.id);
这意味着您的info
对象为null
,而您对item.getMenuInfo()
的调用无效。
这意味着您的HistoryCustomCursorAdapter
未正确设置此值。您尚未发布该对象的代码,但我认为这应该足以让您找到错误。