69

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?

1
  • We cannot help you with seeing your code. Can you post the relevant portions of your Java and XML files?
    – slayton
    Oct 11, 2011 at 14:46

11 Answers 11

285

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.

7
  • This solved my problem, too. I'm kind of surprised that they don't have a method that just wraps Integer.toString(), but whatever.
    – mszaro
    Feb 2, 2013 at 0:50
  • 4
    This is exactly in my case when I try to set text a number into a TextView which is pointed by a reference address generated by Eclipse. If you do so too, then you'll end up changing the target TextView reference address thus generating those error. So you MUST make sure that you don't put a number (or integer) into TextView's setText() method.
    – Aryo
    Feb 24, 2013 at 13:18
  • 1
    Same problem here. I solved putting String.valueof(myInt) inside the setText method May 5, 2014 at 11:46
  • 2
    @androider I used to use myTextView.setText(myInt + ""); myself. But according to THIS it looks like myTextView.setText(Integer.toString(myInt)); is the most efficient way. May 13, 2014 at 15:08
  • I had the same case in Android Studio and it solved! I wonder the reason by the way.
    – Fer
    Jan 22, 2015 at 7:17
41

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.

3
  • 1
    That was all it was... What the hell happens to the project that requires a clean?
    – Bobbake4
    Oct 11, 2011 at 15:02
  • Eclipse gets buggered up quite often when building.
    – dymmeh
    Oct 11, 2011 at 15:16
  • Although I am commenting too late, but I am facing this issue from a couple of days too. I am using Android Studio. I cleaned up my code, invalidate and restart, turn off the machine and restart, but the problem still persists. :( Please help as I am badly stuck.
    – Ali_Waris
    May 12, 2016 at 5:46
26

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()));
3
  • 2
    or just add "" before your int Apr 22, 2015 at 12:44
  • worked for me! They should have implemented it to throw class cast exception or whatever, this is totally misleading Jan 5, 2016 at 14:37
  • same for Toast messages, when you want to show int that you get from your pojo in a toast message Android Studio doesn't warn you Mar 16, 2018 at 6:05
18

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.

1
  • Oh Man nice observation really saved my day :)
    – sumit
    Jun 9, 2017 at 16:07
10

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

Simply call toString(); to fix the problem.

    Integer size = mSomeArray.size();
    textViewReplyCount.setText(size.toString());
1
  • 1
    the problem was solved by adding resultSizeOnUI.setText(((Integer)productSearchResultSize).toString()); Jun 16, 2013 at 22:10
3

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.

0
0

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

0

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

0

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.

0

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.

1
  • Please provide additional details in your answer. As it's currently written, it's hard to understand your solution.
    – Community Bot
    Aug 28, 2021 at 16:16
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.