2

我尝试将以下类添加到我的应用程序中:

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();
    }

}

在 Android 1.6 上运行时,无论何时何地AlertDialogHelper.getDeleteNoteDialog(this, null),我都会收到以下错误:

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;

这适用于 1.6 以上的任何其他版本。老实说,我只在 2.1、2.3 和 4.0 上测试过这个。我认为它也适用于所有其他人(虽然它可能不是真的)。

如果我评论AlertDialogHelper类中的第一个方法(错误抱怨的那个),错误就会发生。但是我需要该方法来处理其他事情,如果我也调用该方法,无论如何都会出现错误。

没有反思的解决方案:

为了解决这个问题,我将以下类作为嵌套类添加到AlertDialogHelper

private static class Compatibility {
    public static AlertDialog.Builder createAlertDialogBuilder(Context context, int alertDialogTheme) {
        return new AlertDialog.Builder(context, alertDialogTheme);
    }
}

然后,在getDarkDialogBuilder方法中,而不是调用这个:

return new AlertDialog.Builder(context, alertDialogTheme);

我称之为:

return Compatibility.createAlertDialogBuilder(context, alertDialogTheme);

这就是我一直在解决类似问题的方式,到目前为止,我对这种方法没有任何问题。

4

2 回答 2

7

我的猜测是getDarkDialogBuilder你在里面调用了两个参数的构造函数AlertDialog.Builder(Context context, int theme)。这是在 API 级别 11 中引入的。对于早期的 API 级别,您只有可用的单参数构造函数:AlertDialog.Builder(Context context).

顺便说一句,如果您发布代码的相关部分,它将提高您从该论坛获得的帮助质量。如果注释掉方法后问题消失了getDarkDialogBuilder,那么您应该发布该方法的整个源代码。

于 2012-03-28T19:20:03.850 回答
0

My guess is, the operation you are performing there is not supported in version 1.6 and introduced in the other versions you tested. Other way to validate this assumption is, go to javadocs for this method and see since:, it shows which version method was introduced.

于 2012-03-28T19:09:58.173 回答