奇怪的错误(找不到方法)在Android 1.6仅 [英] Strange error (could not find method) on Android 1.6 only

查看:289
本文介绍了奇怪的错误(找不到方法)在Android 1.6仅的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着以下类添加到我的应用程序:

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

}

无论何时何地我称之为 AlertDialogHelper.getDeleteNoteDialog(这一点,空)在Android 1.6运行时,我收到以下错误:

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;

这适用于任何其他版本1.6以上就好了。说实话,我只对2.1,2.3和4.0测试这一点。我认为它也适用于所有其他人(可能不是真的,虽然)。

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).

如果我评论在 AlertDialogHelper 类的第一个方法(一个错误抱怨),错误的方式去。但是,我需要其他的东西,方法反正错误显示出来,如果​​我调用该方法了。

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.

解决方案,而反思:

要解决这个问题我已经添加下面的类嵌套类为 AlertDialogHelper

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

然后,在 getDarkDialogBu​​ilder 方法,而不是调用这样的:

Then, in the getDarkDialogBuilder method, instead of calling this:

return new AlertDialog.Builder(context, alertDialogTheme);

我把这叫做:

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.

推荐答案

我的猜测是,在 getDarkDialogBu​​ilder 您呼叫的两个参数的构造函数 AlertDialog.Builder(上下文的背景下,诠释主题)。那是在API级别11引入了对于早期的API级别,你只需要提供的单参数的构造函数: AlertDialog.Builder(上下文的背景下)

My guess is that inside getDarkDialogBuilder you are calling the two-argument constructor AlertDialog.Builder(Context context, int theme). That was introduced in API level 11. For earlier API levels, you only have the single-argument constructor available: AlertDialog.Builder(Context context).

顺便说一句,这将改善的帮助,你从这个论坛得到,如果你发布你的code的相关部分的质量。如果问题消失,当你注释掉方法 getDarkDialogBu​​ilder ,那么你应该张贴该方法的全部源代码。

As an aside, it would improve the quality of help that you get from this forum if you posted the relevant parts of your code. If the problem goes away when you comment out the method getDarkDialogBuilder, then you should post the entire source for the method.

这篇关于奇怪的错误(找不到方法)在Android 1.6仅的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆