0
獲取名字,我有枚舉類像這樣的:從枚舉類
public enum Names {
Logo1(R.drawable.adese,"Adese"),
Logo2(R.drawable.afra,"Afra");
private int resource;
private String description;
private Names(int resource, String description){
this.description = description;
}
public boolean isCorrect(String input){
return input.equals(description);
}
}
我想從它的活動繪製的枚舉的名稱。換句話說,當我爲某種方法提供R.drawable.adese時,我想獲得Logo1。當我提供R.drawable.afra時,我想獲得Logo2等等。我怎樣才能做到這一點?
在我的活動
Names name;
.
.
.
id = R.drawable.adese;
name = Names.forDrawable(id);
check.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
a=name.isCorrect(text.getText().toString());
if(a==true){
Intent intent = new Intent(LogoActivity.this, LevelSelectionActivity.class);
startActivity(intent);
}
}
});
複選按鈕和等在活動中聲明,我在這裏沒有寫縮短。
而且我得到了錯誤的信息:
03-03 12:16:27.125: E/AndroidRuntime(6091): FATAL EXCEPTION: main
03-03 12:16:27.125: E/AndroidRuntime(6091): java.lang.NullPointerException
03-03 12:16:27.125: E/AndroidRuntime(6091): at com.example.turkishlogoquiz.LogoActivity$1.onClick(LogoActivity.java:57)
03-03 12:16:27.125: E/AndroidRuntime(6091): at android.view.View.performClick(View.java:4223)
03-03 12:16:27.125: E/AndroidRuntime(6091): at android.view.View$PerformClick.run(View.java:17275)
03-03 12:16:27.125: E/AndroidRuntime(6091): at android.os.Handler.handleCallback(Handler.java:615)
03-03 12:16:27.125: E/AndroidRuntime(6091): at android.os.Handler.dispatchMessage(Handler.java:92)
03-03 12:16:27.125: E/AndroidRuntime(6091): at android.os.Looper.loop(Looper.java:137)
-03 12:16:27.125: E/AndroidRuntime(6091): at android.app.ActivityThread.main(ActivityThread.java:4898)
03-03 12:16:27.125: E/AndroidRuntime(6091): at java.lang.reflect.Method.invokeNative(Native Method)
03-03 12:16:27.125: E/AndroidRuntime(6091): at java.lang.reflect.Method.invoke(Method.java:511)
03-03 12:16:27.125: E/AndroidRuntime(6091): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1008)
03-03 12:16:27.125: E/AndroidRuntime(6091): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:775)
03-03 12:16:27.125: E/AndroidRuntime(6091): at dalvik.system.NativeStart.main(Native Method)
我這樣做了,但在活動中我遇到了一些活動錯誤。我發佈了一些我的活動和錯誤消息。 – ocry 2013-03-03 10:21:45
我用我的活動和錯誤編輯了我的問題。 – ocry 2013-03-03 10:27:27
ooops ...我發現了錯誤。你需要在'Names'構造函數中分配'resource'字段。 'this.resource = resource;'檢查我更新的答案。 – 2013-03-03 10:29:23