I've tried to add the following class to my app:
public class AlertDialogHelper {
public static AlertDialog.Builder getDarkDialogBuilder(Context context) {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
int alertDialogTheme = AlertDialog.THEME_HOLO_DARK;
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
alertDialogTheme = AlertDialog.THEME_DEVICE_DEFAULT_DARK;
}
return new AlertDialog.Builder(context, alertDialogTheme);
}
return new AlertDialog.Builder(context);
}
public static AlertDialog getDeleteNoteDialog(Context context, OnClickListener deleteListener) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage(R.string.dialog_delete_message);
builder.setPositiveButton(R.string.button_delete, deleteListener);
builder.setNegativeButton(R.string.button_cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
return builder.create();
}
}
Whenever and wherever I call AlertDialogHelper.getDeleteNoteDialog(this, null)
while running on Android 1.6, I get the following error:
03-28 18:56:07.828: E/dalvikvm(303): Could not find method android.app.AlertDialog$Builder.<init>, referenced from method net.ricardoamaral.apps.notificationagenda.AlertDialogHelper.getDarkDialogBuilder
03-28 18:56:07.828: W/dalvikvm(303): VFY: unable to resolve direct method 40: Landroid/app/AlertDialog$Builder;.<init> (Landroid/content/Context;I)V
03-28 18:56:07.828: W/dalvikvm(303): VFY: rejecting opcode 0x70 at 0x0010
03-28 18:56:07.828: W/dalvikvm(303): VFY: rejected Lnet/ricardoamaral/apps/notificationagenda/AlertDialogHelper;.getDarkDialogBuilder (Landroid/content/Context;)Landroid/app/AlertDialog$Builder;
03-28 18:56:07.828: W/dalvikvm(303): Verifier rejected class Lnet/ricardoamaral/apps/notificationagenda/AlertDialogHelper;
This works just fine on any other version above 1.6. To be honest I only tested this on 2.1, 2.3 and 4.0. I assume it also works on all others (it might not be true though).
If I comment the first method in the AlertDialogHelper
class (the one the error is complaining about), the error goes way. But I need that method for other things and the error shows up anyway if I call that method too.
SOLUTION WITHOUT REFLECTION:
To fix the problem I've added the following class as nested-class to AlertDialogHelper
:
private static class Compatibility {
public static AlertDialog.Builder createAlertDialogBuilder(Context context, int alertDialogTheme) {
return new AlertDialog.Builder(context, alertDialogTheme);
}
}
Then, in the getDarkDialogBuilder
method, instead of calling this:
return new AlertDialog.Builder(context, alertDialogTheme);
I call this:
return Compatibility.createAlertDialogBuilder(context, alertDialogTheme);
This is how I've been fixing similar problems and so far I haven't had any issues with this method.