应用程序在选定的上下文菜单上崩溃

时间:2014-07-26 17:55:14

标签: android listview contextmenu expandablelistview

我有2个片段:Fragment_Master_List和Fragment_Trips。 Master_List有一个ExpandableListView,它有一个上下文菜单,这就是我实现它的方式:

@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);
}

到目前为止这是有效的。现在,在我的Fragment_Trips中,我有一个ListView,它还有一个上下文菜单,我按照这样的方式实现:

@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);
}

点击菜单项后 - 应用程序崩溃,我在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)

第121行是这一个:

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

它在Fragment_Master_List中,甚至在Fragment_Trips中都没有......

为什么会发生这种情况?如何解决?

2 个答案:

答案 0 :(得分:0)

例外情况清晰明确:item.getMenuInfo()的结果无法投放到ExpandableListView.ExpandableListContextMenuInfo

您有ListView还是ExpandableListView?显然,菜单信息只是ExpandableListView的ExpandableListContextMenuInfo的一个实例,而不是常规的ListView。

答案 1 :(得分:0)

好的,感谢this回答看起来我解决了这个问题。我不得不重写一些代码:在Fragment_Master_List中,我将片段的上下文菜单的组更改为1:

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

我更改了Fragment_Trips的onCreateContextMenu

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);
}

请注意,此处的组为0。

最后在onContextItemSelected的两个片段中我检查了该组。在Fragment_Master_List中,我在if语句中移动了有问题的行:

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

一切正常!