我有这样的枚举类:
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)