2016-01-24 66 views
1

這個枚舉是枚舉的代碼(內部單例類的):java.lang.ExceptionInInitializerError在具有多個值

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

..... 
} 

UPDATE

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) 
+1

可以分享堆棧跟蹤 – nullpointer

+0

發佈您的Logcat –

+1

'實例'必須爲空,是否已調用getInstance()? –

回答

0

我相信你的代碼在這裏:

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

需要三個值初始化。但是這一行:

public static Category findCategoryById(int mId) 

正在初始化其中的一個。在該靜態上下文中,所有參數都需要初始化。

+0

findCategoryById需要拋出所有的枚舉值然後Category.values()嘗試初始化時失敗...但我不知道它是如何正確初始化在這種情況下 – fabrizotus

-1

問題是CATEGORIA_XX(N,instance.getString(R.string.cat_mmm), 「YY」)

的getString活動需要解釋的getString。

CATEGORIA_XX(N,ActivityInstance.getString(R.string.cat_mmm), 「YY」)

它工作得很好!

非常感謝大家。