android.content.res.Resources $ NotFoundException:资源ID#0x0

时间:2016-08-26 08:33:17

标签: java android android-resources

我尝试使用drawable文件夹中的图片创建自定义视图。 程序从文件夹中选择的图片取决于客户端,因此我需要使用变量来处理R文件。 在做了一项研究之后,我发现做到这一点的方法是这样的:

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);

但是我得到了"的运行时异常; android.content.res.Resources $ NotFoundException:资源ID#0x0"

当我搜索异常时,我得到的答案告诉我要做我在我写的段落中所做的事情。 我做错了什么?

另外,你可以在我添加的代码中看到我插入了一个Log命令进行调试。当我调试该行显示两次。当我只调用一次时,该方法调用两次是否有原因?

我添加了我的代码和堆栈跟踪。

这是该类的一部分(它扩展了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();
    }
}

这是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);

}

这就是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)

谢谢

编辑: 某个视图的xml:

      <com.example.danac.cards.CardView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:id="@+id/testView"/>

1 个答案:

答案 0 :(得分:-1)

String uriName = ":drawable/"+thisCard.getKind()+thisCard.getNumber();
int idUri = getResources().getIdentifier(uriName , null , PACKAGE_NAME);

我猜你的uriName错了。你可以得到你的drawable id:

String drawableName = thisCard.getKind() + thisCard.getNumber();
int idDrawable = getResources().getIdentifier(drawableName, "drawable", PACKAGE_NAME);

您不应将&#34;:drawable&#34; 添加到您的可绘制名称中。您应该将&#34; drawable&#34; 添加到方法getIdentifier。祝你好运。