I'm trying to create a custom view with picture from the drawable folder. the picture that the program choose from the folder depends on the client so I need to approach the R file with a variable. after doing a research I found that the way to do it is like that:
String uriName = ":drawable/"+thisCard.getKind()+thisCard.getNumber();
int idUri = getResources().getIdentifier(uriName , null , PACKAGE_NAME);
Drawable tmp = getResources().getDrawable(idUri , getContext().getTheme());
cardImage.setImageDrawable(tmp);
but I get a RuntimeException of "android.content.res.Resources$NotFoundException: Resource ID #0x0"
when I searched the exception I got answers that told me to do what I did in the paragraph I wrote up. what am I doing wrong?
also, you can see in the code I added down that I inserted a Log command for debug. when I debugging that line shows twice. Is there a reason why that method called twice when I call it only once?
I added my code and the stacktrace.
This is part of the class (it extends View)
public void setCard(Card card){
thisCard = card;
isSet = true;
//in the Card constructor isSet is false so it does not suppose to go in the method setCardImage() yet.
invalidate();
}
private void setCardImage(){
String uriName = ":drawable/"+thisCard.getKind()+thisCard.getNumber();
try {
cardImage = (ImageView)findViewById(R.id.cardImage);
int idUri = getResources().getIdentifier(uriName , null , PACKAGE_NAME);
Drawable tmp = getResources().getDrawable(idUri , getContext().getTheme());
cardImage.setImageDrawable(tmp);
Log.d(TAG , "im here");
}catch(NullPointerException e){
e.printStackTrace();
}
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if(isSet) {
setCardImage();
}
}
this is the MainActivity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
testView = (CardView)findViewById(R.id.testView);
Card card = new Card("club" , 2);
testView.setCard(card);
}
and this is the StackTrace:
08-26 07:54:33.135 2364-2364/com.example.danac.cards E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.danac.cards, PID: 2364
android.content.res.Resources$NotFoundException: Resource ID #0x0
at android.content.res.Resources.getValue(Resources.java:1351)
at android.content.res.Resources.getDrawable(Resources.java:804)
at com.example.danac.cards.CardView.setCardImage(CardView.java:51)
at com.example.danac.cards.CardView.onDraw(CardView.java:65)
at android.view.View.draw(View.java:16178)
at android.view.View.updateDisplayListIfDirty(View.java:15174)
at android.view.View.draw(View.java:15948)
at android.view.ViewGroup.drawChild(ViewGroup.java:3609)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3399)
at android.view.View.updateDisplayListIfDirty(View.java:15169)
at android.view.View.draw(View.java:15948)
at android.view.ViewGroup.drawChild(ViewGroup.java:3609)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3399)
at android.view.View.updateDisplayListIfDirty(View.java:15169)
at android.view.View.draw(View.java:15948)
at android.view.ViewGroup.drawChild(ViewGroup.java:3609)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3399)
at android.view.View.draw(View.java:16181)
at android.view.View.updateDisplayListIfDirty(View.java:15174)
at android.view.View.draw(View.java:15948)
at android.view.ViewGroup.drawChild(ViewGroup.java:3609)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3399)
at android.view.View.updateDisplayListIfDirty(View.java:15169)
at android.view.View.draw(View.java:15948)
at android.view.ViewGroup.drawChild(ViewGroup.java:3609)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3399)
at android.view.View.updateDisplayListIfDirty(View.java:15169)
at android.view.View.draw(View.java:15948)
at android.view.ViewGroup.drawChild(ViewGroup.java:3609)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3399)
at android.view.View.updateDisplayListIfDirty(View.java:15169)
at android.view.View.draw(View.java:15948)
at android.view.ViewGroup.drawChild(ViewGroup.java:3609)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3399)
at android.view.View.updateDisplayListIfDirty(View.java:15169)
at android.view.View.draw(View.java:15948)
at android.view.ViewGroup.drawChild(ViewGroup.java:3609)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3399)
at android.view.View.draw(View.java:16181)
at com.android.internal.policy.PhoneWindow$DecorView.draw(PhoneWindow.java:2690)
at android.view.View.updateDisplayListIfDirty(View.java:15174)
at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:281)
at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:287)
at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:322)
at android.view.ViewRootImpl.draw(ViewRootImpl.java:2615)
at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2434)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2067)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
at android.view.Choreographer.doCallbacks(Choreographer.java:670)
at android.view.Choreographer.doFrame(Choreographer.java:606)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
thank you
EDIT : the xml of that certain view:
<com.example.danac.cards.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/testView"/>
String uriName = ":drawable/"+thisCard.getKind()+thisCard.getNumber();
int idUri = getResources().getIdentifier(uriName , null , PACKAGE_NAME);
I guess your uriName is wrong. You can get id of your drawable like:
String drawableName = thisCard.getKind() + thisCard.getNumber();
int idDrawable = getResources().getIdentifier(drawableName, "drawable", PACKAGE_NAME);
You should not add ":drawable" to your drawable name. You should add "drawable" to method getIdentifier
. Good luck.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.