Android PopupMenu 'could not find android.widget.PopupMenu class'

I want to use a popup menu in my application, which should be compatible with Android 1.6+. Therefore, I use this code (taken from Support for different versions of the platform ) to distinguish between pre-Honeycomb (which does not have PopupMenu ) and Honeycomb + to display a pop-up window or AlertDialog:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { PopupMenu popup = new PopupMenu(this, v); ... } else { showDialog(DIALOG_ID); } 

This works great with emulated Android 2.1 (showing AlertDialog), 2.3.3 (showing AlertDialog) and 4.2.2 (showing PopupMenu). However, it does not work with emulated Android 1.6. I get these error messages:

 E/dalvikvm(211): Could not find class 'android.widget.PopupMenu', referenced from method ... W/dalvikvm(211): VFY: unable to resolve new-instance 50 (Landroid/widget/PopupMenu;) in L...; W/dalvikvm(211): VFY: rejecting opcode 0x22 at 0x0006 W/dalvikvm(211): VFY: rejected L...;.... (Landroid/view/View;)V W/dalvikvm(211): Verifier rejected class L...; W/dalvikvm(211): Class init failed in newInstance call (L...;) 

Why is Android 2.x behaving as expected, but Android 1.6 is not working?

+4
source share
1 answer

See the documentation for this class: http://developer.android.com/reference/android/widget/PopupMenu.html It has been added at API level 11.
Is 1.6 a real requirement for your application? It practically does not exist right now.

You have two solutions:
- Use DialogFragment instead - Create your own implementation based on PopupWindow (as suggested in the document http://developer.android.com/training/backward-compatible-ui/older-implementation.html )

I would suggest revising it, having 1.6 as the lowest goal, if you have no real reason to do this, it will create more problems than it costs literally 0.1% of the installation base. In any case, DialogFragment is a way to use this type of interface element, and it is part of the compatibility library (therefore, it is compatible with older versions of Android).

+4
source

Source: https://habr.com/ru/post/1480227/


All Articles