简体   繁体   中英

java.lang.ClassCastException: android.widget.ExpandableListView$ExpandableListContextMenuInfo

I am really hoping someone will be able to help me here. I am working off of this example: http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/ExpandableList1.html

And I am trying to capture which item invoked the contextMenu. The only example I could find was here:

Detecting which selected item (in a ListView) spawned the ContextMenu (Android)

The only difference is that I have an ExpandableListView instead of a ListView.

When I use the example above from stackoverflow , I receive the following Exception:

02-25 21:08:27.271: E/AndroidRuntime(7812): java.lang.ClassCastException: android.widget.ExpandableListView$ExpandableListContextMenuInfo
02-25 21:08:27.271: E/AndroidRuntime(7812):     at com.frugalicious.android.CategoryList.onCreateContextMenu(CategoryList.java:121)
02-25 21:08:27.271: E/AndroidRuntime(7812):     at android.view.View.createContextMenu(View.java:4362)
02-25 21:08:27.271: E/AndroidRuntime(7812):     at com.android.internal.view.menu.ContextMenuBuilder.show(ContextMenuBuilder.java:81)
02-25 21:08:27.271: E/AndroidRuntime(7812):     at com.android.internal.policy.impl.PhoneWindow$DecorView.showContextMenuForChild(PhoneWindow.java:1880)
02-25 21:08:27.271: E/AndroidRuntime(7812):     at android.view.ViewGroup.showContextMenuForChild(ViewGroup.java:460)
02-25 21:08:27.271: E/AndroidRuntime(7812):     at android.view.ViewGroup.showContextMenuForChild(ViewGroup.java:460)
02-25 21:08:27.271: E/AndroidRuntime(7812):     at android.view.ViewGroup.showContextMenuForChild(ViewGroup.java:460)
02-25 21:08:27.271: E/AndroidRuntime(7812):     at android.widget.AbsListView.performLongPress(AbsListView.java:1874)
02-25 21:08:27.271: E/AndroidRuntime(7812):     at android.widget.AbsListView.access$600(AbsListView.java:74)
02-25 21:08:27.271: E/AndroidRuntime(7812):     at android.widget.AbsListView$CheckForLongPress.run(AbsListView.java:1827)
02-25 21:08:27.271: E/AndroidRuntime(7812):     at android.os.Handler.handleCallback(Handler.java:587)

I will provide any other information needed. Thank you so much in advance.

When I use the example above from stackoverflow , I receive the following Exception

With above statement you mean you literally copy-pasted the answer on the linked question? If so, it's hardly surprising you get a ClassCastException. Try casting to a ExpandableListView.ExpandableListContextMenuInfo , as the exception suggests in stead.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM