简体   繁体   English

Android 资源未找到异常?

[英]Android resource not found exception?

I am having a strange problem using findViewById(id).我在使用 findViewById(id) 时遇到了一个奇怪的问题。 It comes back with resource not found even though the resource is definitely there.即使资源肯定存在,它也会返回未找到资源。 It is a textview in a layout next to another textview, one of the textviews I can find by id but the other shows resource not found.它是另一个文本视图旁边布局中的文本视图,我可以通过 id 找到其中一个文本视图,但另一个显示未找到资源。 Is there any reason this might be happening?是否有任何原因可能会发生这种情况?

确保您不是真的只是试图将文本设置为数字并期望它自动转换为字符串。

Try cleaning your project or post some code.尝试清理您的项目或发布一些代码。

Sometimes the ID's do not get correctly regenerated if you are using Eclipse.如果您使用 Eclipse,有时 ID 无法正确重新生成。 This requires the project to be cleaned and sometimes refreshed.这需要清理项目,有时还需要刷新。

Exception Message thrown is not very descriptive.抛出的异常消息不是很具有描述性。 Its very much likely the case you are trying to cast the int value to String , applying the below change fixed the issue for me.很可能是您尝试将int值转换为String ,应用以下更改为我解决了该问题。

Code before the fix:修复前的代码:

 itemPrice.setText(foodMenuItems.get(position).getItemPrice());

Code after the fix:修复后的代码:

 itemPrice.setText(Integer.toString(foodMenuItems.get(position).getItemPrice()));

Just to clarify Terra Caines answer since I saw it happening a lot to people;只是为了澄清 Terra Caines 的回答,因为我看到人们经常遇到这种情况; TextView , and other text components, have 2 setText() functions with 1 parameter. TextView和其他文本组件有 2 个带有 1 个参数的setText()函数。

One of them is with a String and one with int.其中一个是String ,一个是 int。 The int is obviously for a string Resource such as R.string.myString - which, to those who didnt know, R.exm always is represented as int . int显然用于字符串资源,例如R.string.myString - 对于那些不知道的人来说, R.exm总是表示为int The string is for putting a string there.字符串用于将字符串放在那里。

So for example I want to put int x = 1;所以例如我想把 int x = 1; in a textView.在文本视图中。 Doing mTextView.setText(x);mTextView.setText(x); will cause the textView to use the resource function and since there is probably no resource with the id 1 it will throw the exception Resource not found.将导致 textView 使用资源函数,并且由于可能没有 id 为 1 的资源,它将抛出异常 Resource not found。 If You want to put an int or any number in the setText() Function make sure to convert it to String (x+"") or (x.toString()) would do the trick for you.如果您想在 setText() 函数中放置一个 int 或任何数字,请确保将其转换为String (x+"")(x.toString())将为您解决问题。

Hope it saved some time to people.希望它为人们节省了一些时间。

textViewCount.setText(someArray.size()); was my problem.是我的问题。

Simply call toString();只需调用toString(); to fix the problem.解决问题。

    Integer size = mSomeArray.size();
    textViewReplyCount.setText(size.toString());

Make sure you don't set any attributes programmaticly which are not available.确保您没有以编程方式设置任何不可用的属性。 I had the very same problem and the reason was a RemoteView.setFloat(id,"setWeight",1.0f);我有同样的问题,原因是RemoteView.setFloat(id,"setWeight",1.0f); to a LinearLayout , which was not supported with Android before 4.x Unfortunately the error message was not very helpful on this.到 4.x 之前的 Android 不支持的LinearLayout不幸的是,错误消息对此没有太大帮助。

In my case, I had int variable ( on top of my Activity class ) and I was trying to pass that int variable as second parameter to Toast.makeText() ( to show that to the user ), I don't know I was stupid or android dev platform is!就我而言,我有一个int变量(在我的 Activity 类之上),我试图将该 int 变量作为第二个参数传递给Toast.makeText()向用户展示),我不知道我是愚蠢的或android开发平台是! :p :p

检查清单文件的应用程序标签下android:icon="@mipmap/ic_launcher"存在android:icon="@mipmap/ic_launcher"属性。

I my case, I was trying to pass drawable selector with item android:color to background of view.我的情况是,我试图将带有项目 android:color 的可绘制选择器传递给视图背景。 The problem here is that you cannot define the background color using a color selector, you need a drawable selector.这里的问题是你不能使用颜色选择器定义背景颜色,你需要一个可绘制的选择器。

In my case In the res/menu/(xml file) In that xml file, in the there I used a png icon which was causing error you should only use vector icon.就我而言,在 res/menu/(xml 文件) 中,在那个 xml 文件中,我使用了导致错误的 png 图标,您应该只使用矢量图标。 This error was showing in android 6(marshmallow) and not in higher versions.此错误显示在 android 6(marshmallow) 而不是更高版本中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM