枚举实现接口被验证程序拒绝(java.lang.VerifyError) [英] Enums implementing interface are rejected by verifier (java.lang.VerifyError)

查看:131
本文介绍了枚举实现接口被验证程序拒绝(java.lang.VerifyError)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将枚举用于不同物理量的单位,例如英里 $ c> DISTANCE 。要以通用方式使用它们,请使用接口 Unit ,该接口的方法为 convert(double)

I'm using enums for units of different physical quantities, like meter and miles for the DISTANCE. To use them in a generic way there is an interface Unit which has the method convert(double).

要加载首选单位,请使用单例:

To load the preferred units, a singleton is used:

public class UnitPreferences {

    private static UnitPreferences sInstance;

    private HashMap<PhysicalQuantity, Unit> mUnits;

    /**
     * Returns the instance of the preferred units collection.
     *
     * @param context the context
     * @return Instance of the preferred units collection.
     */
    public static UnitPreferences from(Context context) {
        if (sInstance == null) {
            sInstance = new UnitPreferences(context);
        }
        return sInstance;
    }

    /**
     * Creates a new set of preferred units by fetching them from the Shared Preferences.
     *
     * @param context the resources
     */
    private UnitPreferences(Context context) {
        // Load the preferred units from SharedPreferences and put them in the mUnits HashMap
    }

    /**
     * Returns all units of a specific physical quantity.
     *
     * @param physicalQuantity the physical quantity
     * @return All units available to this physical quantity.
     */
    private Unit[] getAllUnits(PhysicalQuantity physicalQuantity) {
        switch (physicalQuantity) {
            case DISTANCE:
                return DistanceUnit.values();

            // others...

            default:
                throw new RuntimeException("No units defined for " + physicalQuantity);
        }
    }

    /**
     * Returns the preferred unit of a physical quantity.
     *
     * @param phQuantity the physical quantity
     * @return The preferred unit.
     */
    public Unit getPreferredUnit(PhysicalQuantity phQuantity) {
        return mUnits.get(phQuantity);
    }
}

PhysicalQuantity 枚举:

public enum PhysicalQuantity {

    DISTANCE,
    // others...

}

单位接口:

public interface Unit {

    double convert(double value);

}

DistanceUnit 实现 Unit 接口:

public enum DistanceUnit implements Unit {

    KILOMETER(R.string.unit_km, "km"),
    MILES(R.string.unit_mi, "mi");

    public static final double KM_PER_MI = 1.609344d;

    private int label;
    private String id;

    DistanceUnit(int label, String id) {
        this.label = label;
        this.id = id;
    }

    @Override
    public double convert(double meters) {
        double km = meters / 1000d;
        if (this == MILES) return km / KM_PER_MI;
        return km;
    }
}



问题:



我第一次使用单位时会引发异常:

The Problem:

The exception is thrown the first time I'm using the units:

Unit distanceUnit = UnitPreferences.from(context).getPreferredUnit(DISTANCE);

当我实现它时,一切都很好,现在,在将其合并到主数据库中后,我得到了 VerifyError

When I implemented it everything worked fine, now after it was merged into the master I'm getting a VerifyError

java.lang.VerifyError: Verifier rejected class com.example.app.UnitPreferences: com.example.app.Unit[]
  com.example.app.UnitPreferences.getAllUnits(com.example.app.PhysicalQuantity) failed to verify:
  com.example.app.units.Unit[] com.example.app.UnitPreferences.getAllUnits(com.example.app.PhysicalQuantity):
  [0x28] returning 'Reference: java.lang.Enum[]', but expected from declaration 'Reference: com.example.app.Unit[]'
  (declaration of 'com.example.app.UnitPreferences' in /data/app/com.example.app-2/base.apk:classes32.dex)

我已经清理并重建了几次并关闭了Instant Run。有人可以给我提示如何解决此错误吗?

I've already cleaned and rebuild several times and turned off Instant Run. Can anyone give me a hint how to fix this error?

推荐答案

只看了一点堆栈跟踪,它说:

Just looked into the stacktrace a little more and it says:

returning 'Reference: Enum[]', but expected from declaration 'Reference: Unit[]'

getAllUnits()

将返回的枚举 getAllUnits()铸造为 Unit [] 可以手动解决此问题,尽管存在提示:

Casting the returned enums of getAllUnits() to Unit[] manually fixed the issue although there is a hint:

Casting 'DistanceUnit.values()' to 'Unit[]' is redundant

< img src = https://i.stack.imgur.com/xxoGW.png alt =铸造单元[]手动>

这篇关于枚举实现接口被验证程序拒绝(java.lang.VerifyError)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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