java - Import image from Drawable imageResource Exception -
after trying on stackoverflow , didnt find want ask.
i created func import image drawable folder , display on imageview.(my db have 218 pictures orderd id 0)
code:(after explain problem)
private void pic_view(string pic2) { // todo auto-generated method stub //גישה לדגל לפי שמו וייבוא התמונה log.d("result 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); }
when imageresource beacame(geting zero) 0 got fatal exception dont know why. didnt find way import image drawable. conclusion: why imageresource getting 0 hope can me figure out
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
stacktrace:
04-25 21:21:44.420: d/result 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 resource number 0x00000000 04-25 21:21:44.420: e/activitythread(16327): failed 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): @ android.content.res.resources.getvalue(resources.java:1195) 04-25 21:21:44.420: e/activitythread(16327): @ android.content.res.resources.getdrawable(resources.java:729) 04-25 21:21:44.420: e/activitythread(16327): @ android.content.res.resources.getdrawable(resources.java:711) 04-25 21:21:44.420: e/activitythread(16327): @ com.example.flagsgame.classicmode.pic_view(classicmode.java:221) 04-25 21:21:44.420: e/activitythread(16327): @ com.example.flagsgame.classicmode.oncreate(classicmode.java:82) 04-25 21:21:44.420: e/activitythread(16327): @ android.app.activity.performcreate(activity.java:5231) 04-25 21:21:44.420: e/activitythread(16327): @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1087) 04-25 21:21:44.420: e/activitythread(16327): @ android.app.activitythread.performlaunchactivity(activitythread.java:2169) 04-25 21:21:44.420: e/activitythread(16327): @ android.app.activitythread.handlelaunchactivity(activitythread.java:2271) 04-25 21:21:44.420: e/activitythread(16327): @ android.app.activitythread.handlerelaunchactivity(activitythread.java:3762) 04-25 21:21:44.420: e/activitythread(16327): @ android.app.activitythread.access$900(activitythread.java:144) 04-25 21:21:44.420: e/activitythread(16327): @ android.app.activitythread$h.handlemessage(activitythread.java:1211) 04-25 21:21:44.420: e/activitythread(16327): @ android.os.handler.dispatchmessage(handler.java:102) 04-25 21:21:44.420: e/activitythread(16327): @ android.os.looper.loop(looper.java:136) 04-25 21:21:44.420: e/activitythread(16327): @ android.app.activitythread.main(activitythread.java:5146) 04-25 21:21:44.420: e/activitythread(16327): @ java.lang.reflect.method.invokenative(native method) 04-25 21:21:44.420: e/activitythread(16327): @ java.lang.reflect.method.invoke(method.java:515) 04-25 21:21:44.420: e/activitythread(16327): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:796) 04-25 21:21:44.420: e/activitythread(16327): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:612) 04-25 21:21:44.420: e/activitythread(16327): @ 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 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 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): @ android.app.activitythread.performlaunchactivity(activitythread.java:2212) 04-25 21:21:44.440: e/androidruntime(16327): @ android.app.activitythread.handlelaunchactivity(activitythread.java:2271) 04-25 21:21:44.440: e/androidruntime(16327): @ android.app.activitythread.handlerelaunchactivity(activitythread.java:3762) 04-25 21:21:44.440: e/androidruntime(16327): @ android.app.activitythread.access$900(activitythread.java:144) 04-25 21:21:44.440: e/androidruntime(16327): @ android.app.activitythread$h.handlemessage(activitythread.java:1211) 04-25 21:21:44.440: e/androidruntime(16327): @ android.os.handler.dispatchmessage(handler.java:102) 04-25 21:21:44.440: e/androidruntime(16327): @ android.os.looper.loop(looper.java:136) 04-25 21:21:44.440: e/androidruntime(16327): @ android.app.activitythread.main(activitythread.java:5146) 04-25 21:21:44.440: e/androidruntime(16327): @ java.lang.reflect.method.invokenative(native method) 04-25 21:21:44.440: e/androidruntime(16327): @ java.lang.reflect.method.invoke(method.java:515) 04-25 21:21:44.440: e/androidruntime(16327): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:796) 04-25 21:21:44.440: e/androidruntime(16327): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:612) 04-25 21:21:44.440: e/androidruntime(16327): @ 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): @ android.content.res.resources.getvalue(resources.java:1195) 04-25 21:21:44.440: e/androidruntime(16327): @ android.content.res.resources.getdrawable(resources.java:729) 04-25 21:21:44.440: e/androidruntime(16327): @ android.content.res.resources.getdrawable(resources.java:711) 04-25 21:21:44.440: e/androidruntime(16327): @ com.example.flagsgame.classicmode.pic_view(classicmode.java:221) 04-25 21:21:44.440: e/androidruntime(16327): @ com.example.flagsgame.classicmode.oncreate(classicmode.java:82) 04-25 21:21:44.440: e/androidruntime(16327): @ android.app.activity.performcreate(activity.java:5231) 04-25 21:21:44.440: e/androidruntime(16327): @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1087) 04-25 21:21:44.440: e/androidruntime(16327): @ android.app.activitythread.performlaunchactivity(activitythread.java:2169) 04-25 21:21:44.440: e/androidruntime(16327): ... 12 more
javadoc for
public int getidentifier (string name, string deftype, string defpackage)
says:
returns associated resource identifier. returns 0 if no such resource found. (0 not valid resource id.)
which means of drawable
's names store in database incorrect , not correspond real names have in drawable
folder. please verify these names.
Comments
Post a Comment