40

I'm getting crash reports from android marketplace:

android.content.res.Resources$NotFoundException: Resource ID #0x....

I get about 17 of these a week. It's pointing me to the following in my code:

context.getResources().getDrawable(R.drawable.foo);

That resource is definitely present in my /drawable folder. I have several hundred thousand installs, I'm not sure how this could be failing for some users, but working for the vast majority. I'd like to find out what's going on, because they can't use the app in this state. Any ideas?

Thanks

--------- Update ----------------------

Also I can see the entry for the drawable in question in my R.java file, looks like:

public static final int foo=0x7f020034;

I do a clean build of the project, then straight after that do my release build (no code modification in between to give the automatic eclipse build stuff possibly let the R file go awry)

Thanks

user291701
  • 38,411
  • 72
  • 187
  • 285
  • This can be obvious, but do you have the "Build Automatically" check in Eclipse selected? If you don't use Eclipse, did you regenerate the resource classes after modifying them? Can you show us your R class? – m0skit0 Feb 27 '12 at 16:30
  • Hi yes I do have "build automatically" checked, but I make sure to do a clean build before every release. I can see the resource entry present for that drawable in R.java (pasted above) Thank you – user291701 Feb 27 '12 at 16:39
  • I don't see anything pasted, but if you say the entry is there... Are the package shown in R correct (same as your Activity)? – m0skit0 Feb 27 '12 at 16:41
  • Hey, yes same package. If the package was wrong or the entry were missing in the R file, I was thinking that all the other installs would also be crashing. It happens to just a few users, can't understand why, so frustrating! – user291701 Feb 27 '12 at 16:51
  • I get about 80 a week right now. I can not see why! This is being caused from loads of all and any of my activities, not just one specific resource. Obviously we go through a full regression testing before release and never see this issue. Where could it be coming from? Is this a bug in android? It is being seen on ALL types of device and supported OS (2.2+). – Aiden Fry Feb 21 '13 at 16:10
  • It is also happening in my app as well. As from my experience and from my user complains, this issue occurs on QVGA devices. i ran my app on QVGA emulator, same exception occur in onCreate() method where i was setting layout for my splash screen. – Mohammad Imran Feb 21 '13 at 20:06
  • Some devices are using layouts which you didn't provide, and the drawables/layouts are 'missing' ... This is a device-specific issue, nothing you can do about it until you get that device and iron it out on it. – Shark Feb 28 '13 at 15:18
  • @Shark This sounds promising- except this is being reported on some devices which I DO have. Now, could this be being caused because I am loading a layout such as main.... except this layout is NOT included in the standard layout folder instead it is included within layout-normal-port, layout-large-port and layout-xlarge-port & land (I have set the orientation so it must fit within one of these) - could it be because the devices are unable to figure out their own configurations and try taking the resource from the layout folder? – Aiden Fry Mar 01 '13 at 14:33
  • I'll post this lengtty thing in the answer. – Shark Mar 01 '13 at 20:54
  • Possible duplicate of [Getting android.content.res.Resources$NotFoundException: exception even when the resource is present in android](http://stackoverflow.com/questions/9161435/getting-android-content-res-resourcesnotfoundexception-exception-even-when-the) – rds May 09 '16 at 12:24

9 Answers9

52

I got this exception:

Resources$NotFoundException: String resource ID

when I was using setText with an int value. I had to convert it to String.

Before:

myTextView.setText(obj.SomeIntProperty);    

After:

myTextView.setText(String.valueOf(obj.SomeIntProperty));
live-love
  • 48,840
  • 22
  • 240
  • 204
12

Is the crash reports coming from version <=1.6, and you have certain resources only in qualified folders, i.e. "drawable-mdpi" instead of just "drawable"? If so then read about "Known issues" at the bottom of this page.

pgsandstrom
  • 14,361
  • 13
  • 70
  • 104
  • Ah dang I do have some resources in /drawable-mdpi and others just in /drawable. I've been running this on a 1.6 emulator for some time though, and never had any issue with it (my min target is 1.6, so users shouldn't even be able to run on 1.5). Could that be it? Shouldn't it be crashing on the emulator then, too? – user291701 Feb 27 '12 at 16:46
  • Yes, it should crash on the emulator as well. I'm using an 1.6-emulator myself to track down these kind of bugs. If you have tested carefully with the emulator, then you can exclude this bug :/ – pgsandstrom Feb 27 '12 at 16:48
  • Hmm yes actually I see now that the marketplace is saying that this error is happening on Nexus One, Droid, then others. I think Nexus One was running minimum 2.0, same for Droid. Argh. – user291701 Feb 27 '12 at 16:50
  • This sounds closest to 'home' IMO. – Shark Feb 28 '13 at 15:18
2

The same problem I found on my application today morning and found a very simple solution to this. the problem looks big and complicated but it's not.

Steps which I followed are

  1. Delete the bin folder it self
  2. Clean the project
  3. Remove application from your device
  4. Run the application and check your scenario.

According to me it should not give this exception again.

Reason : When you build the application R.java contains all the memory address and it happens that those address are invalid for next run.

Second Reason : When you have the application which supports lanscape and portrati mode both and if you have not wrote the XML file for either of them then it's the possibility that may lead to this kind of crash. and there are pretty much chances of OutoFmemory and Memory leak issues when you have to support both the modes. please think on this as well.

Dinesh Prajapati
  • 9,274
  • 5
  • 30
  • 47
  • 1
    For me anyway - this is not useful. I am getting these crash reports from live projects on the app store. I have never seen this during testing or my own usage of the live version using various devices (which are the same devices/os's as some of the reports being sent it). This does not seem to be related to one specific resource/address - for me - as I have seen crash reports when getting all manner of resources - could this be caused by the OS throwing OutOfMemory exceptions? If so I am not getting OOM crashed / stack traces. – Aiden Fry Feb 28 '13 at 12:07
  • 1
    So are you saying that - This could be being caused because I am loading a layout such as main.... except this layout is NOT included in the standard layout folder instead it is included within layout-normal-port, layout-large-port and layout-xlarge-port & land (I have set the orientation so it must fit within one of these) - could it be because the devices are unable to figure out their own configurations and try taking the resource from the layout folder? 0- interestingly i am seeing some reports on devices which i Have tested on. – Aiden Fry Mar 01 '13 at 14:37
  • 1
    Yes. exactly what happens if application trys to load the XML from the respective folder while the orientation is changed but some times if the xml is not places it gives this kind of exception – Dinesh Prajapati Mar 02 '13 at 04:47
  • Hmmm cheers, ill do this and hope it solves it. I have never seen this issue on my devices! – Aiden Fry Mar 05 '13 at 10:12
2

This have occurred several times especially on phones with low density. I also noticed that it mostly happens with 9-patch images.

My solution was to include 9-patch images in as much density resource folders as I could (mdpi, ldpi, hdpi, xdpi).

Sherif elKhatib
  • 45,786
  • 16
  • 89
  • 106
2

You have included certain codes that reference content from Android SDK.

For example, I faced a similar issue once and when I removed the line

android:background="?android:attr/actionBarItemBackground"

from my xml, everything works fine again.

Neoh
  • 15,906
  • 14
  • 66
  • 78
2

In my case I had a layout file that lead to the crash:

I got the following log-

enter image description here

That showed this link to my actual code:

enter image description here

Which is this line of code:

enter image description here

I checked my file navigator folder names as suggested by the most popular answer and it looked like the resource that is supposedly missing is in the normal layout folder

enter image description here

but when looking under the Project file navigator rather than the Android file navigator I was able to see that not only was the supposedly missing resource file not in the normal layout folder, but I didn't even have a layout folder without a special extension.

enter image description here

Adding an Android Resource Directory to the res folder and calling it layout and then pasting over the resources from your speciality folder (ex. layout-sw720dp) into it fixed the issue for me.

enter image description here

This solution will also work for other folders as suggested such as not having files in drawable and only having them in drawable-xxhdpi can be fixed by moving the file into drawable and tweaking it as needed. Good luck folder fiends!

Chris Klingler
  • 5,258
  • 2
  • 37
  • 43
1

Heh, no idea, I'm actually working on a similar issue, but it could be it. It's worthwhile mentioning that while the device can upscale resources and layouts, it cannot downscale them. So if you have minimum values, set them in the AndroidManifest.xml. Try adding layout-ldpi and layout-small first.

A useful technique for logging which layout got loaded is to attach an android:tag to every root container of your layout XML file, and in onCreate() after setting setContentView(R.id.layout) just print the value of the tag. It will tell you which got loaded - you still have to plug the hole by adding all possible combinations, if just for debug purposes.

also, rename your drawables to drawables-nodpi folder to ensure no drawables are missing. this turns off internal scaling, makes APKs smaller and doesn't "pick" from any other drawable folder.

once you figure out whether it's the layout or drawable, additional metrics will help you find the root cause. for now, check the layouts first with "always use" drawables.

on monday when i get back to the office I'll post you the metrics code used to measure data captured in this thread - Which part of Android is in charge of picking a correct resource profile?

Community
  • 1
  • 1
Shark
  • 6,513
  • 3
  • 28
  • 50
0

This could also happen if the resource you are referring to (lets call it ResA) is in-turn referring to a resource which is missing (lets call it ResB). Android will raise the ResourceNotFoundException for ResA even though whats really missing is ResB. Such is life!

In my case, ResB was defined in the values-swxxxdp but not in values. Hence I was getting this exception on phones but not on tablets.

pareshgoel
  • 981
  • 8
  • 12
0

The error can happen when portrait XML layout file is not defined. It doesn't happen all the time, but happens for example if user quickly turns off and on the phone, I suppose in that case the system calls the onCreate in portrait orientation, before calling onCreate in landscape.

Paolo
  • 2,461
  • 5
  • 31
  • 45