Android resource not found exception?

JavaAndroid

Java Problem Overview


I am having a strange problem using 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. Is there any reason this might be happening?

Java Solutions


Solution 1 - Java

Make sure that you aren't really just trying to set the text to a number and expecting it to automatically convert to a string.

Solution 2 - Java

Try cleaning your project or post some code.

Sometimes the ID's do not get correctly regenerated if you are using Eclipse. This requires the project to be cleaned and sometimes refreshed.

Solution 3 - Java

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.

Code before the fix:

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

Code after the fix:

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

Solution 4 - Java

Just to clarify Terra Caines answer since I saw it happening a lot to people; TextView, and other text components, have 2 setText() functions with 1 parameter.

One of them is with a String and one with 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. The string is for putting a string there.

So for example I want to put int x = 1; in a textView. Doing 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. 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.

Hope it saved some time to people.

Solution 5 - Java

textViewCount.setText(someArray.size()); was my problem.

Simply call toString(); to fix the problem.

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

Solution 6 - Java

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); to a LinearLayout, which was not supported with Android before 4.x Unfortunately the error message was not very helpful on this.

Solution 7 - Java

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! :p

Solution 8 - Java

Check if the property android:icon="@mipmap/ic_launcher" exists under the application tag of your manifest file.

Solution 9 - Java

I my case, I was trying to pass drawable selector with item android:color to background of view. The problem here is that you cannot define the background color using a color selector, you need a drawable selector.

Solution 10 - Java

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. This error was showing in android 6(marshmallow) and not in higher versions.

Solution 11 - Java

Check if you have

<?xml version="1.0" encoding="utf-8"?>

declared twice in any of the resource files.

I had two of them in a file, removed one, and it worked like a charm.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBobbake4View Question on Stackoverflow
Solution 1 - JavaTerra CainesView Answer on Stackoverflow
Solution 2 - JavaMike dgView Answer on Stackoverflow
Solution 3 - JavaRohit GuptaView Answer on Stackoverflow
Solution 4 - JavaShaiView Answer on Stackoverflow
Solution 5 - JavaYoungjaeView Answer on Stackoverflow
Solution 6 - JavaJürgen 'Kashban' WahlmannView Answer on Stackoverflow
Solution 7 - JavaMehdi DehghaniView Answer on Stackoverflow
Solution 8 - JavaSyed Arsalan KazmiView Answer on Stackoverflow
Solution 9 - JavaDmitry AndroidView Answer on Stackoverflow
Solution 10 - JavaSamuel Aktar LaskarView Answer on Stackoverflow
Solution 11 - JavaAditya GuptaView Answer on Stackoverflow