68

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?

Bobbake4
  • 24,509
  • 9
  • 59
  • 94
  • We cannot help you with seeing your code. Can you post the relevant portions of your Java and XML files? – slayton Oct 11 '11 at 14:46

11 Answers11

283

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.

Terra Caines
  • 2,995
  • 2
  • 15
  • 8
  • 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 02 '13 at 00: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 '13 at 13:18
  • 1
    Same problem here. I solved putting String.valueof(myInt) inside the setText method – androidevil May 05 '14 at 11:46
  • 2
    @androider I used to use `myTextView.setText(myInt + "");` myself. But according to [THIS](http://stackoverflow.com/questions/653990/what-is-the-most-efficient-way-to-convert-an-int-to-a-string) it looks like `myTextView.setText(Integer.toString(myInt));` is the most efficient way. – Kevin Cruijssen May 13 '14 at 15:08
  • I had the same case in **Android Studio** and it solved! I wonder the reason by the way. – Fer Jan 22 '15 at 07:17
  • My case was just the opposite, I was trying to assign an Integer into a TextView. Thanks a lot for saving my day. :) – orchidrudra Apr 22 '16 at 13:32
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.

Mike dg
  • 4,648
  • 2
  • 27
  • 26
  • 1
    That was all it was... What the hell happens to the project that requires a clean? – Bobbake4 Oct 11 '11 at 15:02
  • Eclipse gets buggered up quite often when building. – dymmeh Oct 11 '11 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 '16 at 05: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()));
Rohit Gupta
  • 593
  • 5
  • 8
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.

Winter
  • 3,894
  • 7
  • 24
  • 56
Shai
  • 318
  • 6
  • 15
10

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

Simply call toString(); to fix the problem.

    Integer size = mSomeArray.size();
    textViewReplyCount.setText(size.toString());
Youngjae
  • 24,352
  • 18
  • 113
  • 198
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.

khagler
  • 3,996
  • 29
  • 40
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

Mehdi Dehghani
  • 10,970
  • 6
  • 59
  • 64
0

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

Syed Arsalan Kazmi
  • 949
  • 1
  • 11
  • 17
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.

  • Please provide additional details in your answer. As it's currently written, it's hard to understand your solution. – Community Aug 28 '21 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.