Reputation: 481
I want to use a popup menu in my app, which should be compatible with Android 1.6+. Therefore I am using this code (taken from Supporting Different Platform Versions) to distinguish between pre-Honeycomb (which has no PopupMenu) and Honeycomb+ to display either a PopupMenu or an AlertDialog:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
PopupMenu popup = new PopupMenu(this, v);
...
} else {
showDialog(DIALOG_ID);
}
This works fine with an emulated Android 2.1 (showing AlertDialog), 2.3.3 (showing AlertDialog) and 4.2.2 (showing PopupMenu). However it does not work with an 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 does Android 2.x behave as expected but Android 1.6 does not?
Upvotes: 4
Views: 2168
Reputation: 4389
Look at the documentation of 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 app ? It is virtually non existent right now.
You have two solutions :
- Use a DialogFragment instead
- Create your own implementation based on PopupWindow (as suggested in the doc http://developer.android.com/training/backward-compatible-ui/older-implementation.html)
I would suggest to reconsider having 1.6 as the lowest target if you don't have a real reason to do so, it will create more trouble than it is worth for literally 0.1% of the install base. In any case, DialogFragment is the way to go for this kind of UI elements, and it is part of the compatibility library (so it is compatible with old versions of Android).
Upvotes: 4