【问题标题】:App crashes on context menu selected所选上下文菜单上的应用程序崩溃
【发布时间】:2014-07-26 17:55:13
【问题描述】:

我有 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 中...

为什么会发生,我该如何解决?

【问题讨论】:

  • 现在我想知道为什么这个问题被否决了?它有什么问题?

标签: android listview contextmenu expandablelistview


【解决方案1】:

异常说得很清楚:item.getMenuInfo() 的结果不能转换为ExpandableListView.ExpandableListContextMenuInfo

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

【讨论】:

  • 但是我该如何解决呢?如果我删除演员表 - 我有演员表错误并建议将演员表添加到 (ExpandableListContextMenuInfo)
  • 我在 Fragment_Trips 中有一个 ListView,在 Fragment_Master_List 中有一个 ExpandableListView
  • 尝试在抛出异常的行设置断点并检查 item.mMenuInfo(我猜它必须像这样命名)以找出 getMenuInfo() 返回值的实际类型。
  • 我只是在那条线上得到一个info cannot be resolved to a variable,价值"<error(s)_during_the_evaluation>。我仍然想知道,从 Fragment_Trips 调用上下文菜单时,我为什么会在 Fragment_Master_List 中出现异常?
  • 我找到了解决方案,发布了答案。感谢您尝试帮助我,不胜感激!
【解决方案2】:

好的,感谢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...
    }
}

一切正常!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多