Crashed when deleting photo in CrimeFragment


#1

I don’t what causes the crash. I also add a delete context menu for deleting suspect chosen, but it does no work, either.

Looking for help!


#2

check your crashing log, find the crashing method.

may be there are two onContextItemSelected(MenuItem item), one in CrimeListFragment and one in CrimeFragment.

the FragmentManagement can’t decide which one to use, we can use switch (item.getItemId()) to decide, but need to move

                AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
                int position = info.position;
                CrimeAdapter adapter = (CrimeAdapter) getListAdapter();
                Crime crime = adapter.getItem(position);

inside the switch clause

in CrimeListFragment.java

@Override
    public boolean onContextItemSelected(MenuItem item) {

        switch (item.getItemId()) {
            case R.id.context_menu_item_delete_crime:

                // move into the switch clause
                AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
                int position = info.position;
                CrimeAdapter adapter = (CrimeAdapter) getListAdapter();
                Crime crime = adapter.getItem(position);

                CrimeLab.getCrimeLab(getActivity()).deleteCrime(crime);
//                adapter.notifyDataSetChanged();
                updateUI();
                return true;
        }

        return super.onContextItemSelected(item);
    }