1

在我的应用程序中有一个列表视图,在操作栏中有一个删除按钮。ListView 包含复选框。当我按下删除按钮时,它应该检查 ListView 中所有选中的项目,如果有,它应该删除这些项目。我的代码是这样的:

case R.id.discard: 
            SparseBooleanArray checkedItemPositions = getListView().getCheckedItemPositions();
            int itemCount = getListView().getCount();

            for(int i=itemCount-1; i >= 0; i--){
                if(checkedItemPositions.get(i)){
                    AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
                    mDbHelper.deleteReminder(info.id);
                    fillData();
                }
            }
            checkedItemPositions.clear();
            mDbHelper.notify();
        return super.onMenuItemSelected(featureId, item);}
        return false;

问题是当我在我的应用程序中按下按钮时,应用程序不幸崩溃了。这是我的 LogCat:

08-22 15:41:59.789: E/AndroidRuntime(704): FATAL EXCEPTION: main
08-22 15:41:59.789: E/AndroidRuntime(704): java.lang.NullPointerException
08-22 15:41:59.789: E/AndroidRuntime(704):  at com.example.girtsreminder.ReminderListActivity.onMenuItemSelected(ReminderListActivity.java:76)
08-22 15:41:59.789: E/AndroidRuntime(704):  at com.actionbarsherlock.ActionBarSherlock.callbackOptionsItemSelected(ActionBarSherlock.java:604)
08-22 15:41:59.789: E/AndroidRuntime(704):  at com.actionbarsherlock.internal.ActionBarSherlockCompat.onMenuItemSelected(ActionBarSherlockCompat.java:516)
08-22 15:41:59.789: E/AndroidRuntime(704):  at com.actionbarsherlock.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:744)
08-22 15:41:59.789: E/AndroidRuntime(704):  at com.actionbarsherlock.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:148)
08-22 15:41:59.789: E/AndroidRuntime(704):  at com.actionbarsherlock.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:885)
08-22 15:41:59.789: E/AndroidRuntime(704):  at com.actionbarsherlock.internal.view.menu.ActionMenuView.invokeItem(ActionMenuView.java:510)
08-22 15:41:59.789: E/AndroidRuntime(704):  at com.actionbarsherlock.internal.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:146)
08-22 15:41:59.789: E/AndroidRuntime(704):  at android.view.View.performClick(View.java:2408)
08-22 15:41:59.789: E/AndroidRuntime(704):  at android.view.View$PerformClick.run(View.java:8816)
08-22 15:41:59.789: E/AndroidRuntime(704):  at android.os.Handler.handleCallback(Handler.java:587)
08-22 15:41:59.789: E/AndroidRuntime(704):  at android.os.Handler.dispatchMessage(Handler.java:92)
08-22 15:41:59.789: E/AndroidRuntime(704):  at android.os.Looper.loop(Looper.java:123)
08-22 15:41:59.789: E/AndroidRuntime(704):  at android.app.ActivityThread.main(ActivityThread.java:4627)
08-22 15:41:59.789: E/AndroidRuntime(704):  at java.lang.reflect.Method.invokeNative(Native Method)
08-22 15:41:59.789: E/AndroidRuntime(704):  at java.lang.reflect.Method.invoke(Method.java:521)
08-22 15:41:59.789: E/AndroidRuntime(704):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-22 15:41:59.789: E/AndroidRuntime(704):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-22 15:41:59.789: E/AndroidRuntime(704):  at dalvik.system.NativeStart.main(Native Method)

有人可以帮我解决这个问题,然后我可以更轻松地从列表视图中删除项目。?

更新:

import com.actionbarsherlock.app.SherlockListActivity;

import android.app.ActionBar;
import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.SparseBooleanArray;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.SimpleCursorAdapter;
import android.widget.AdapterView.AdapterContextMenuInfo;

public class ReminderListActivity extends SherlockListActivity {
    private static final int ACTIVITY_CREATE=0;
    private static final int ACTIVITY_EDIT=1;

    private RemindersDbAdapter mDbHelper;




    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.reminder_list);
        mDbHelper = new RemindersDbAdapter(this);
        mDbHelper.open();
        fillData();
        registerForContextMenu(getListView());

        com.actionbarsherlock.app.ActionBar aBar = getSupportActionBar();
        String dateString = (String) android.text.format.DateFormat.format("MMM d, yyyy", new java.util.Date());
        aBar.setTitle(dateString);


    }

    @Override
    public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
        // TODO Auto-generated method stub
         com.actionbarsherlock.view.MenuInflater inflater = getSupportMenuInflater();
            inflater.inflate(R.menu.menu, (com.actionbarsherlock.view.Menu) menu);
        return super.onCreateOptionsMenu(menu);
    }



    @Override
    public boolean onMenuItemSelected(int featureId,
            com.actionbarsherlock.view.MenuItem item) {
        // TODO Auto-generated method stub
        switch(item.getItemId()) {
        case R.id.add: 
            createReminder();
            return true; 
        case R.id.discard: 
            SparseBooleanArray checkedItemPositions = getListView().getCheckedItemPositions();
            int itemCount = getListView().getCount();

            for(int i=itemCount-1; i >= 0; i--){
                if(checkedItemPositions.get(i)){
                    AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
                    mDbHelper.deleteReminder(info.id);
                    fillData();
                }
            }
            checkedItemPositions.clear();
            mDbHelper.notify();
        return super.onMenuItemSelected(featureId, item);}
        return false;
    }



    private void fillData() {
        Cursor remindersCursor = mDbHelper.fetchAllReminders();
        startManagingCursor(remindersCursor);

        // Create an array to specify the fields we want to display in the list (only TITLE)
        String[] from = new String[]{RemindersDbAdapter.KEY_TITLE};

        // and an array of the fields we want to bind those fields to (in this case just text1)
        int[] to = new int[]{R.id.text1};

        // Now create a simple cursor adapter and set it to display
        SimpleCursorAdapter reminders = 
                new SimpleCursorAdapter(this, R.layout.text, remindersCursor, from, to);
        setListAdapter(reminders);



    }





    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,
            ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        MenuInflater mi = getMenuInflater(); 
        mi.inflate(R.menu.list_menu_item_longpress, menu); 
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        switch(item.getItemId()) {
        case R.id.menu_delete:
            AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
            mDbHelper.deleteReminder(info.id);
            fillData();
            return true;
        }
        return super.onContextItemSelected(item);
    }



    private void createReminder() {
        Intent i = new Intent(this, ReminderEditActivity.class);
        startActivityForResult(i, ACTIVITY_CREATE);
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        Intent i = new Intent(this, ReminderEditActivity.class);
        i.putExtra(RemindersDbAdapter.KEY_ROWID, id);
        startActivityForResult(i, ACTIVITY_EDIT); 
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
        fillData();
    }


}
4

0 回答 0