gpt4 book ai didi

具有多个值的枚举中的 java.lang.ExceptionInInitializerError

转载 作者:行者123 更新时间:2023-12-01 06:10:53 25 4
gpt4 key购买 nike

这是枚举的代码(在单例类内部):

public class GlobalStore extends Application{
.....
......
.....
public synchronized static GlobalStore getInstance() {
if(instance==null){
instance = new GlobalStore();
}
return instance;
}

.....
......

public static enum Category {
CATEGORIA_DB(1,instance.getString(R.string.cat_dep_bel),"db"),
CATEGORIA_EL(2,instance.getString(R.string.cat_edu_lec),"el"),
CATEGORIA_E(3,instance.getString(R.string.cat_entre),"e"),
CATEGORIA_EM(4,instance.getString(R.string.cat_est_mod),"em"),
CATEGORIA_GV(5,instance.getString(R.string.cat_gas_vinos),"gv"),
CATEGORIA_DH(6,instance.getString(R.string.cat_dis_hog),"dh"),
CATEGORIA_S(7,instance.getString(R.string.cat_salud),"s"),
CATEGORIA_ST(8,instance.getString(R.string.cat_ser_tec),"st"),
CATEGORIA_T(9,instance.getString(R.string.cat_turis),"t"),
CATEGORIA_AC(10,instance.getString(R.string.cat_arte_cultura),"ac");


private final int id;
private final String gloss;
private final String code;

Category(int mId, String mGloss, String mCode) {
this.id = mId;
this.gloss = mGloss;
this.code = mCode;
}

public int getId() {
return id;
}

public String getGloss() {
return gloss;
}

public String getImagen() {
return "icon_"+code;
}

public String getSelectedImagen() {
return this.getImagen() + "_red";
}

public String getDrawString() {
return "mn_"+code+"1";
}

}

以及此行中的错误:

public static Category findCategoryById(int mId) {

for (Category categoria : Category.values()) { //HERE is error in Category.values() -- line 327
if (categoria.getId()==mId) {
return categoria;
}
}
return null;
}

.....
}

更新

logcat 中的错误:

01-24 05:42:49.310 31800-31800/? E/AndroidRuntime: FATAL EXCEPTION: main
01-24 05:42:49.310 31800-31800/? E/AndroidRuntime: Process: com.assertsoft.beneplus.alpha, PID: 31800
01-24 05:42:49.310 31800-31800/? E/AndroidRuntime: java.lang.ExceptionInInitializerError
01-24 05:42:49.310 31800-31800/? E/AndroidRuntime: at com.assertsoft.model.GlobalStore.findCategoriaById(GlobalStore.java:327)
01-24 05:42:49.310 31800-31800/? E/AndroidRuntime: at com.assertsoft.adapters.ListaCuponesAdapter.getView(ListaCuponesAdapter.java:196)

最佳答案

我相信你的代码:

Category(int mId, String mGloss, String mCode) {
this.id = mId;
this.gloss = mGloss;
this.code = mCode;
}

期望初始化三个值。但这行:

public static Category findCategoryById(int mId)

正在初始化其中之一。在该静态上下文中,所有参数都需要初始化。

关于具有多个值的枚举中的 java.lang.ExceptionInInitializerError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34972099/

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