尝试在stackoverflow上查找它并且没有找到我想问。
我创建了一个func,它将从我的Drawable文件夹中导入一个图像并将其显示在Imageview上。(我的数据库有218张图片,所有图片都是按0从ID开始订购)
代码 :(之后我会解释问题)
private void pic_view(String pic2) {
// TODO Auto-generated method stub
//גישה לדגל לפי שמו וייבוא התמונה
Log.d("Result from pic function " , pic2);
ImageView imageView = (ImageView)findViewById(R.id.imageView1);
String uri ="@drawable/";
uri += pic2;
int imageResource = getResources().getIdentifier(uri, pic2, getPackageName());//הצוות התמונה
Log.d("Imageresources", String.valueOf(imageResource));//get 0 problem
//if(imageResources!=0)
Drawable res= getResources().getDrawable(imageResource);//ציור התמונה
imageView.setImageDrawable(res);
}
当imageResource beacame(geting zero)为零时,我得到了一个致命的异常,我不知道为什么。 我没有找到另一种从drawable导入图像的方法。 结论: 为什么imageResource变为零希望你可以帮我搞清楚
DataBase(ID 0):
db.addFlag(new Flags("afghanistan",0,"Afghanistan"));
db.addFlag(new Flags("albania",1,"albania"));
db.addFlag(new Flags("algeria",2,"Algeria" ));
OnCreat :
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
ActionBar actionBar = getActionBar();// הכנסת פעולה ל ActionBar
actionBar.setDisplayHomeAsUpEnabled(true);
score =(TextView)findViewById(R.id.score); // הגדרת כפתורים ושדות טקסטד
scorenum =(TextView)findViewById(R.id.scorenum);
scorenum.setText(String.valueOf(s.score));
guess =(TextView)findViewById(R.id.guesses);
numOfGuess=(TextView)findViewById(R.id.numOfGuesses);
numOfGuess.setText(String.valueOf(Guesses.numOfGuesses));
hint =(Button)findViewById(R.id.hint);
hint.setOnClickListener(hintOnClickListener);
mpHint = MediaPlayer.create(this,R.raw.hint_sound); // הגדרת סאונדים
mpNext = MediaPlayer.create(this, R.raw.next_flag);
mpWrong = MediaPlayer.create(this, R.raw.wrong_answer);
animationfadein = AnimationUtils.loadAnimation(this, R.anim.fade_in);// הגדרת אנימציות
animationfadeout = AnimationUtils.loadAnimation(this, R.anim.fade_out);
Flags f = new Flags();
Random r = new Random();//הדגל שיבחר לשאלה
int num = r.nextInt(160);//Up
f = db.getFlag(num);//הצגת הדגל הרנדומלי שיצא
fn = f.getName().toString();
pic = f.getImage().toString();
pic_view(pic);//מעבר לפונקציה להשמת התמונה של הדגל במשחק
//מערך ארבע כפתורים כנגד ארבע תשובות
b[0] = (Button)findViewById(R.id.button1);
b[1] = (Button)findViewById(R.id.button2);
b[2] = (Button)findViewById(R.id.button3);
b[3] = (Button)findViewById(R.id.button4);
List<String>Answers=new ArrayList<String>();//מערך תשובות
Answers.add(f.getName().toString());//הוספת התשובה הנכונה
for(int i=1;i<4;i++)
{
num = r.nextInt(200);
String valToAdd1 = db.getFlag(num).getName().toString();
if(!Answers.contains(valToAdd1)/*&&!valToAdd1.equals("Button")*/){
WrongAnswers.add(valToAdd1);
Answers.add(valToAdd1);
}
}
/*if(WrongAnswers.size()<2){
}*/
Collections.shuffle(Answers);//ערבוב התשובות
for(int i=0;i<Answers.size();i++)
{
b[i].setText(Answers.get(i));//השמת התשובות מהמהערך למערך הכפתורים
b[i].startAnimation(animationfadein);
}
}//end of OnCreat
堆栈跟踪:
04-25 21:21:44.420: D/Result from pic function(16327): burkina
04-25 21:21:44.420: D/Imageresources(16327): 0
04-25 21:21:44.420: W/ResourceType(16327): No package identifier when getting value for resource number 0x00000000
04-25 21:21:44.420: E/ActivityThread(16327): Failed to find resource
04-25 21:21:44.420: E/ActivityThread(16327): android.content.res.Resources$NotFoundException: Resource ID #0x0
04-25 21:21:44.420: E/ActivityThread(16327): at android.content.res.Resources.getValue(Resources.java:1195)
04-25 21:21:44.420: E/ActivityThread(16327): at android.content.res.Resources.getDrawable(Resources.java:729)
04-25 21:21:44.420: E/ActivityThread(16327): at android.content.res.Resources.getDrawable(Resources.java:711)
04-25 21:21:44.420: E/ActivityThread(16327): at com.example.flagsgame.ClassicMode.pic_view(ClassicMode.java:221)
04-25 21:21:44.420: E/ActivityThread(16327): at com.example.flagsgame.ClassicMode.onCreate(ClassicMode.java:82)
04-25 21:21:44.420: E/ActivityThread(16327): at android.app.Activity.performCreate(Activity.java:5231)
04-25 21:21:44.420: E/ActivityThread(16327): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-25 21:21:44.420: E/ActivityThread(16327): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2169)
04-25 21:21:44.420: E/ActivityThread(16327): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2271)
04-25 21:21:44.420: E/ActivityThread(16327): at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3762)
04-25 21:21:44.420: E/ActivityThread(16327): at android.app.ActivityThread.access$900(ActivityThread.java:144)
04-25 21:21:44.420: E/ActivityThread(16327): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1211)
04-25 21:21:44.420: E/ActivityThread(16327): at android.os.Handler.dispatchMessage(Handler.java:102)
04-25 21:21:44.420: E/ActivityThread(16327): at android.os.Looper.loop(Looper.java:136)
04-25 21:21:44.420: E/ActivityThread(16327): at android.app.ActivityThread.main(ActivityThread.java:5146)
04-25 21:21:44.420: E/ActivityThread(16327): at java.lang.reflect.Method.invokeNative(Native Method)
04-25 21:21:44.420: E/ActivityThread(16327): at java.lang.reflect.Method.invoke(Method.java:515)
04-25 21:21:44.420: E/ActivityThread(16327): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:796)
04-25 21:21:44.420: E/ActivityThread(16327): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:612)
04-25 21:21:44.420: E/ActivityThread(16327): at dalvik.system.NativeStart.main(Native Method)
04-25 21:21:44.420: D/AndroidRuntime(16327): Shutting down VM
04-25 21:21:44.420: W/dalvikvm(16327): threadid=1: thread exiting with uncaught exception (group=0x41aeedb8)
04-25 21:21:44.440: E/AndroidRuntime(16327): FATAL EXCEPTION: main
04-25 21:21:44.440: E/AndroidRuntime(16327): Process: com.example.flagsgame, PID: 16327
04-25 21:21:44.440: E/AndroidRuntime(16327): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.flagsgame/com.example.flagsgame.ClassicMode}: android.content.res.Resources$NotFoundException: Resource ID #0x0
04-25 21:21:44.440: E/AndroidRuntime(16327): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2212)
04-25 21:21:44.440: E/AndroidRuntime(16327): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2271)
04-25 21:21:44.440: E/AndroidRuntime(16327): at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3762)
04-25 21:21:44.440: E/AndroidRuntime(16327): at android.app.ActivityThread.access$900(ActivityThread.java:144)
04-25 21:21:44.440: E/AndroidRuntime(16327): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1211)
04-25 21:21:44.440: E/AndroidRuntime(16327): at android.os.Handler.dispatchMessage(Handler.java:102)
04-25 21:21:44.440: E/AndroidRuntime(16327): at android.os.Looper.loop(Looper.java:136)
04-25 21:21:44.440: E/AndroidRuntime(16327): at android.app.ActivityThread.main(ActivityThread.java:5146)
04-25 21:21:44.440: E/AndroidRuntime(16327): at java.lang.reflect.Method.invokeNative(Native Method)
04-25 21:21:44.440: E/AndroidRuntime(16327): at java.lang.reflect.Method.invoke(Method.java:515)
04-25 21:21:44.440: E/AndroidRuntime(16327): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:796)
04-25 21:21:44.440: E/AndroidRuntime(16327): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:612)
04-25 21:21:44.440: E/AndroidRuntime(16327): at dalvik.system.NativeStart.main(Native Method)
04-25 21:21:44.440: E/AndroidRuntime(16327): Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x0
04-25 21:21:44.440: E/AndroidRuntime(16327): at android.content.res.Resources.getValue(Resources.java:1195)
04-25 21:21:44.440: E/AndroidRuntime(16327): at android.content.res.Resources.getDrawable(Resources.java:729)
04-25 21:21:44.440: E/AndroidRuntime(16327): at android.content.res.Resources.getDrawable(Resources.java:711)
04-25 21:21:44.440: E/AndroidRuntime(16327): at com.example.flagsgame.ClassicMode.pic_view(ClassicMode.java:221)
04-25 21:21:44.440: E/AndroidRuntime(16327): at com.example.flagsgame.ClassicMode.onCreate(ClassicMode.java:82)
04-25 21:21:44.440: E/AndroidRuntime(16327): at android.app.Activity.performCreate(Activity.java:5231)
04-25 21:21:44.440: E/AndroidRuntime(16327): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-25 21:21:44.440: E/AndroidRuntime(16327): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2169)
04-25 21:21:44.440: E/AndroidRuntime(16327): ... 12 more
答案 0 :(得分:0)
的Javadoc
public int getIdentifier (String name, String defType, String defPackage)
表示:
Returns the associated resource identifier. Returns 0 if no such resource was found. (0 is not a valid resource ID.)
这意味着您存储在数据库中的某些drawable
名称不正确,并且与drawable
文件夹中的实名不对应。请验证这些名称。