我想在我的应用程序中使用一个弹出菜单,它应该与 Android 1.6+ 兼容。因此,我使用此代码(取自Supporting Different Platform Versions)来区分 pre-Honeycomb (没有PopupMenu)和 Honeycomb+ 以显示 PopupMenu 或 AlertDialog:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
PopupMenu popup = new PopupMenu(this, v);
...
} else {
showDialog(DIALOG_ID);
}
这适用于模拟的 Android 2.1(显示 AlertDialog)、2.3.3(显示 AlertDialog)和 4.2.2(显示 PopupMenu)。但是它不适用于模拟的 Android 1.6。我收到这些错误消息:
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...;)
为什么 Android 2.x 的行为符合预期,而 Android 1.6 却没有?