0

I have 2 Fragments: Fragment_Master_List and Fragment_Trips. Master_List has an ExpandableListView which has a context menu, this is how I implemented it:

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    ExpandableListView.ExpandableListContextMenuInfo info 
        = (ExpandableListView.ExpandableListContextMenuInfo) menuInfo;

    int type = ExpandableListView.getPackedPositionType(info.packedPosition);
    int groupPosition = ExpandableListView.getPackedPositionGroup(info.packedPosition);
    int childPosition = ExpandableListView.getPackedPositionChild(info.packedPosition);

    group_to_manipulate = arr_all_groups.get(groupPosition);

    // Show context menu for groups
    if (type == ExpandableListView.PACKED_POSITION_TYPE_GROUP) {
        menu.setHeaderTitle(group_to_manipulate.getGroup_name());
        menu.add(0, Constants.Context_Menu_Items.CONTEXT_MENU_EDIT, 0, "Edit"));
        menu.add(0, Constants.Context_Menu_Items.CONTEXT_MENU_DELETE, 1, "Delete"));
        menu.add(0, Constants.Context_Menu_Items.CONTEXT_MENU_DELETE_ALL, 1, "Delete all"));
        menu.add(0, Constants.Context_Menu_Items.CONTEXT_MENU_CLOSE, 2, "Close");

        // Show context menu for children
    } else if (type == ExpandableListView.PACKED_POSITION_TYPE_CHILD) {
        bi_to_manipulate = group_to_manipulate.getGroup_items().get(childPosition);
        menu.setHeaderTitle(bi_to_manipulate.getItem_name());
        menu.add(0, Constants.Context_Menu_Items.CONTEXT_MENU_EDIT, 0, "Edit");
        menu.add(0, Constants.Context_Menu_Items.CONTEXT_MENU_DELETE, 1, "Delete");
        menu.add(0, Constants.Context_Menu_Items.CONTEXT_MENU_DELETE_ALL, 1, "Delete all");
        menu.add(0, Constants.Context_Menu_Items.CONTEXT_MENU_CLOSE, 2, "Close");
    }
}

@Override
public boolean onContextItemSelected(MenuItem item) {
    ExpandableListView.ExpandableListContextMenuInfo info 
        = (ExpandableListView.ExpandableListContextMenuInfo) item.getMenuInfo();

    int type = ExpandableListView.getPackedPositionType(info.packedPosition);
    int groupPosition = ExpandableListView.getPackedPositionGroup(info.packedPosition);
    int childPosition = ExpandableListView.getPackedPositionChild(info.packedPosition);

    Group g = arr_all_groups.get(groupPosition);
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    AlertDialog dialog;

    if (type == ExpandableListView.PACKED_POSITION_TYPE_GROUP) {
        switch (item.getItemId())
        {
        case Constants.Context_Menu_Items.CONTEXT_MENU_DELETE:
            //do something to delete
            break;
        case Constants.Context_Menu_Items.CONTEXT_MENU_DELETE_ALL:
            //do something to delete all
            break;
        case Constants.Context_Menu_Items.CONTEXT_MENU_EDIT:
            //edit code
            break;
        }

    } else if (type == ExpandableListView.PACKED_POSITION_TYPE_CHILD) {
        Base_Item bi = g.getGroup_items().get(childPosition);
        switch (item.getItemId())
        {
        case Constants.Context_Menu_Items.CONTEXT_MENU_DELETE:
            //delete code
            break;
        case Constants.Context_Menu_Items.CONTEXT_MENU_DELETE_ALL:
            //delete all code
            break;
        case Constants.Context_Menu_Items.CONTEXT_MENU_EDIT:
            //edit code
            break;
        }
    }
    return super.onContextItemSelected(item);
}

So far this works. Now, in my Fragment_Trips I have a ListView, which also has a context menu, which I implement like that:

@Override
public void onCreateContextMenu(ContextMenu menu, View v,
        ContextMenuInfo menuInfo) {

    MenuInflater inflater = Fragment_Trips.this.getActivity().getMenuInflater();
    inflater.inflate(R.menu.context_menu, menu);

    AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
    trip_to_manipulate = (Trip) lv_trips.getItemAtPosition(info.position);
    menu.setHeaderTitle(trip_to_manipulate.getTrip_name());

    super.onCreateContextMenu(menu, v, menuInfo);
}

Once I click on a menu item - the app crashes and I'm getting the following error messages in LogCat:

07-26 20:10:36.606: E/AndroidRuntime(9967): FATAL EXCEPTION: main
07-26 20:10:36.606: E/AndroidRuntime(9967): java.lang.ClassCastException: android.widget.AdapterView$AdapterContextMenuInfo cannot be cast to android.widget.ExpandableListView$ExpandableListContextMenuInfo
07-26 20:10:36.606: E/AndroidRuntime(9967):     at com.mycompany.myapp.fragments.Fragment_Master_List.onContextItemSelected(Fragment_Master_List.java:121)
07-26 20:10:36.606: E/AndroidRuntime(9967):     at android.support.v4.app.Fragment.performContextItemSelected(Fragment.java:1601)
07-26 20:10:36.606: E/AndroidRuntime(9967):     at android.support.v4.app.FragmentManagerImpl.dispatchContextItemSelected(FragmentManager.java:2008)
07-26 20:10:36.606: E/AndroidRuntime(9967):     at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:375)
07-26 20:10:36.606: E/AndroidRuntime(9967):     at com.android.internal.policy.impl.PhoneWindow$DialogMenuCallback.onMenuItemSelected(PhoneWindow.java:3521)
07-26 20:10:36.606: E/AndroidRuntime(9967):     at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
07-26 20:10:36.606: E/AndroidRuntime(9967):     at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:149)
07-26 20:10:36.606: E/AndroidRuntime(9967):     at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
07-26 20:10:36.606: E/AndroidRuntime(9967):     at com.android.internal.view.menu.MenuDialogHelper.onClick(MenuDialogHelper.java:167)
07-26 20:10:36.606: E/AndroidRuntime(9967):     at com.android.internal.app.AlertController$AlertParams$3.onItemClick(AlertController.java:924)
07-26 20:10:36.606: E/AndroidRuntime(9967):     at android.widget.AdapterView.performItemClick(AdapterView.java:292)
07-26 20:10:36.606: E/AndroidRuntime(9967):     at android.widget.AbsListView.performItemClick(AbsListView.java:1058)
07-26 20:10:36.606: E/AndroidRuntime(9967):     at android.widget.AbsListView$PerformClick.run(AbsListView.java:2514)
07-26 20:10:36.606: E/AndroidRuntime(9967):     at android.widget.AbsListView$1.run(AbsListView.java:3168)
07-26 20:10:36.606: E/AndroidRuntime(9967):     at android.os.Handler.handleCallback(Handler.java:605)
07-26 20:10:36.606: E/AndroidRuntime(9967):     at android.os.Handler.dispatchMessage(Handler.java:92)
07-26 20:10:36.606: E/AndroidRuntime(9967):     at android.os.Looper.loop(Looper.java:137)
07-26 20:10:36.606: E/AndroidRuntime(9967):     at android.app.ActivityThread.main(ActivityThread.java:4424)
07-26 20:10:36.606: E/AndroidRuntime(9967):     at java.lang.reflect.Method.invokeNative(Native Method)
07-26 20:10:36.606: E/AndroidRuntime(9967):     at java.lang.reflect.Method.invoke(Method.java:511)
07-26 20:10:36.606: E/AndroidRuntime(9967):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-26 20:10:36.606: E/AndroidRuntime(9967):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-26 20:10:36.606: E/AndroidRuntime(9967):     at dalvik.system.NativeStart.main(Native Method)

Line 121 is this one:

ExpandableListView.ExpandableListContextMenuInfo info 
    = (ExpandableListView.ExpandableListContextMenuInfo) item.getMenuInfo();

and it's in Fragment_Master_List, not even in Fragment_Trips...

Why is it happening and how do I fix it?

Igal
  • 5,833
  • 20
  • 74
  • 132

2 Answers2

0

The exception says it loud and clear: The result of item.getMenuInfo() cannot be casted to ExpandableListView.ExpandableListContextMenuInfo.

Do you have a ListView or an ExpandableListView? Obviously the menu info is only an instance of ExpandableListContextMenuInfo for an ExpandableListView, not for a regular ListView.

Ridcully
  • 23,362
  • 7
  • 71
  • 86
  • But how do I fix it then? If I remove the cast - I have a cast error and a suggestion to add the cast to `(ExpandableListContextMenuInfo)`. – Igal Jul 26 '14 at 18:11
  • I have a `ListView` in Fragment_Trips and `ExpandableListView` in Fragment_Master_List. – Igal Jul 26 '14 at 18:12
  • Try setting a breakpoint at the line that throws the exception and inspect item.mMenuInfo (I guess it has to be named like this) to find out the actual type of the value getMenuInfo() returns. – Ridcully Jul 26 '14 at 18:15
  • I'm just getting a `info cannot be resolved to a variable` on that line, and in value `"`. I still wonder, how come I'm getting an exception in Fragment_Master_List when calling for context menu from Fragment_Trips? – Igal Jul 26 '14 at 18:34
  • I found a solution, posted an answer. Thanks for trying to help me, appreciate it! – Igal Jul 26 '14 at 19:18
0

OK, thanks to this answer looks like I solved the issue. I had to rewrite some code: in Fragment_Master_List I changed the group for the fragment's context menu to 1:

menu.add(0, Constants.Context_Menu_Items.CONTEXT_MENU_EDIT, 0, "Edit"));

I changed the onCreateContextMenu for my Fragment_Trips:

public void onCreateContextMenu(ContextMenu menu, View v,
        ContextMenuInfo menuInfo) {

    AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
    trip_to_manipulate = (Trip) lv_trips.getItemAtPosition(info.position);
    menu.setHeaderTitle(trip_to_manipulate.getTrip_name());

    menu.add(0, Constants.Context_Menu_Items.CONTEXT_MENU_EDIT, 0, "Edit");
    menu.add(0, Constants.Context_Menu_Items.CONTEXT_MENU_DELETE, 1, "Delete");
    menu.add(0, Constants.Context_Menu_Items.CONTEXT_MENU_DELETE_ALL, 1, "Delete all");
    menu.add(0, Constants.Context_Menu_Items.CONTEXT_MENU_CLOSE, 3, "Close");
    super.onCreateContextMenu(menu, v, menuInfo);
}

Notice that the group here is 0.

And finally in onContextItemSelected in both fragments I checked for the group. In Fragment_Master_List I moved the problematic line inside the if statement:

public boolean onContextItemSelected(MenuItem item) {
    if(item.getGroupId()==1)
    {
        ExpandableListView.ExpandableListContextMenuInfo info 
            = (ExpandableListView.ExpandableListContextMenuInfo) item.getMenuInfo();
        //rest of the code...
    }
}

And everything worked!

Community
  • 1
  • 1
Igal
  • 5,833
  • 20
  • 74
  • 132