gpt4 book ai didi

java - 安卓.content.res.Resources$NotFoundException : Resource ID #0x0

转载 作者:太空狗 更新时间:2023-10-29 15:25:50 24 4
gpt4 key购买 nike

我正在尝试使用可绘制文件夹中的图片创建自定义 View 。程序从文件夹中选择的图片取决于客户端,因此我需要使用变量处理 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: Resource ID #0x0”的 RuntimeException

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

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

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

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

这是主要 Activity :

 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)

谢谢

编辑:该特定 View 的 xml:

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

我猜你的 uriName 是错误的。您可以像这样获取可绘制对象的 ID:

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

您不应将 ":drawable" 添加到您的可绘制对象名称中。您应该将 "drawable" 添加到方法 getIdentifier 中。祝你好运。

关于java - 安卓.content.res.Resources$NotFoundException : Resource ID #0x0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39161864/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com