java - 枚举实现接口(interface)被 validator 拒绝 (java.lang.VerifyError)

标签 java android enums

我使用枚举来表示不同物理量的单位,例如 英里 表示距离。要以通用方式使用它们,有一个接口(interface) Unit,它具有方法 convert(double)

为了加载首选单位,使用单例:

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

}

Unit 接口(interface):

public interface Unit {

    double convert(double value);

}

实现 Unit 接口(interface)的 DistanceUnit:

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

问题:

第一次使用单位时抛出异常:

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

当我实现它时,一切工作正常,现在将其合并到主版本中后,我收到一个 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)

我已经清理和重建了几次并关闭了即时运行。谁能给我提示如何修复此错误?

最佳答案

只是进一步查看了堆栈跟踪,它显示:

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

getAllUnits()

getAllUnits() 返回的枚举强制转换为 Unit[] 手动修复了该问题,尽管有提示:

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

Casting Unit[] manually

关于java - 枚举实现接口(interface)被 validator 拒绝 (java.lang.VerifyError),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45168175/

相关文章:

java - 如何在没有 if 语句的情况下做出决定

enums - 枚举类型作为输入或输出

编译编辑好的apk后Android apk安装报错

android - 第一个 Android 应用程序 : R cannot be resolved to a variable?

android - Android 中的眼动追踪

c# - C#中的枚举类型

java - 为什么在 android 标准类中使用 static final int 来避免枚举?

java - 我将如何循环回到开头?

java - spring-data-mongodb 可选查询参数

java - 如何配置 VisualizationViewer 以便用户可以使用鼠标移动节点?