gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 20:19:27 27 4
gpt4 key购买 nike

我使用枚举来表示不同物理量的单位,例如 英里 表示距离。要以通用方式使用它们,有一个接口(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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com