getDrawable returns null - android

Pff, this is one of these things that ruins your schedule, but you don't wanna give up. Since 2 days some of my bitmap resources don't load and show up in my app anymore. I nailed it down to the getDrawable() returning null on the supplied ResID.
I'm running this on Genymotion Nexus 5 with API 22, or a Nexus 5 physical device. For my project I mainly use the following to load drawables.
ResourcesCompat.getDrawable(context.getResources(), drawableID, null);
But because of this issue I also tried context.getDrawable(id) and getResources().getDrawable(id, getTheme()). Each method returns null.
Other things that might be interesting:
Resource is in my project (in mdpi, hdpi, xhdpi && xxhdpi)
file-name's are correct (xxx_yyy_zzz.png)
Clean project / deleted build products
Got old image files from my repo: Doesn't load
Resource have been copied and renamed: Doesn't load.
Open the resources in my Image Editor and saved as brand new files: Doesn't load.
Copied resources into empty dummy project: They actually show!
90% of my resources load perfectly, it's just a few larger bitmaps given issues.
The image does show in Android Studio Layout preview (Nexus 5, Api 22)
I've included some tests (like the copies), occasionally they actually load, but that's rare. The image I really care about never loads.
I also tried stepping into the Android Code and get these on getDrawable(id):
throw new IllegalStateException("Override configuration has already been set") (ContextThemeWrapper.java)
in AssetManager.java , getResourceValue I see a parameter being passed of type TypedValue with a string "res/drawable-xxhdpi-v4/{image_name}.png"
The latter raises some alarm bells as well, cause I don't have a folder with that -v4 suffix, so I wonder why it creates that. But stepping through that code in Android Studio doesn't really make sense to me as stepping into a method usually drops me right in a comment block of Android.
Anyway, I hope someone had some weird issues with image resources as well and might be able to point me in the right direction as to why this is happening. The fact that the image isn't corrupt and I really don't see anything wrong in my method calling or resources-folders, makes my kinda out of options.
Thanks in advance.

The only time this has ever happened to me was when I placed the image I was trying to load into my mipmap folder instead of the normal drawables folder.
When you begin typing R.drawable. can you see your drawable?

Related

Puzzled Over Android App Graphic Source Path

I've had an app developed and have the responsibility of maintaining it, which means learning the Eclipse ADT environment. Nearly 20 years in web dev gives me some comfort, but this is certainly a new experience.
In one of the screens shown in the Graphical Layout window, a graphic source is indicated in the Properties panel as:
Src #drawable/ordo_search
ordo_search, obviously being the name of the PNG graphic, drawable appearing to be the folder.
But there are 4 folders holding graphics for this app, all beginning with the word drawable. They are:
drawable
drawable-hdpi
drawable-large-mdpi
drawable-sw600dp-hdpi
By altering this particular image and seeing the change come up in the Graphical Layout, I've determined that this graphic resides in the one called drawable-sw600dp-hdpi. In other areas of the app, I've determined in the same way that graphics are being pulled from any of the 4 folders, but in all cases the properties source paths all read the same: #drawable
Somewhere that #drawable attribute is being told an absolute path to where that graphic is, and that's what I need to find: where would I find and edit the path to that, or any, graphic?
Obviously I'm just getting to know the environment, so bear with me if you would.
It's not possible to get the path
This path will differ from device to device due to different dpi's of devices, it can point to any of the 4 folders you defined. If you want the drawable image you can get it via code by using getResources().getDrawable(R.drawable.yourdrawablename);
This will return your drawable and you can use it to display in a ImageView or where ever you want.

Resources NotFoundException?

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
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));
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.
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
Delete the bin folder it self
Clean the project
Remove application from your device
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.
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).
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.
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?
In my case I had a layout file that lead to the crash:
I got the following log-
That showed this link to my actual code:
Which is this line of code:
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
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.
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.
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!
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.
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.

The Infamous "Exceeds VM Budget" Issue

So I have an APP that loops and continuously draws many png files to a canvas. In the constructor for the thread, for some of the pngs I declare Drawable and some Bitmap handles and assign them (respectively) like so:
Drawablename = context.getResources().getDrawable(R.drawable.pngresource);
mBackgroundImage = BitmapFactory.decodeResource(res, R.drawable.bckgrnd);
Keep in mind I do both methods, MANY times. (even though most of the images are fairly small)
Well... the problem i'm getting is that when trying to start this app on older devices (specifically like the original droid and older) it force closes with the VM budget error.
From research, I've noticed that this seems to be a common problem. (The app runs fine on all newer devices such as droid x, every tablet at best buy, charge, atrix, etc.)
So my question is could I be doing something better?
Is one of these methods of referencing the pngs superior?
Also What exactly is happening here? I need to be able to reference the Image to draw at any time. In other words, at any given instant I could call Draw on one of the handles.
The fixes I've seen for similar problems involve calling the Garbage collector, but would that help me since I would need the Images later anyway? Or is this exclusively a problem with the way i'm referencing the pngs from the drawable folder?
Sorry if this is confusing, i'm a beginner.
If I called System.gc() after every reference would that help, even though the reference is still stored as a Drawable object?
The common fix:
Resize image size, for example: createScaledBitmap()...
Reduce image quality, Config.inSampleSize setting...
Remove all references to Bitmap objects when un-used (setting references to NULL, of course). If you don't do this, System.gc() will does nothing, remember this! and this method call does not guarantee that the memory will be freed right away.

Is there a limit on the size or number of Drawable resources in an android project?

I'm attempting to use .png files I've created in paint.net as drawable resources to draw to a canvas, but I've noticed a problem...
I tried creating a few .png's that are 500x500px I copied them into the res folder like normal, got no errors and it compiled fine.
However, when the app is running it's supposed to draw the image as 100x100px.
Well when the drawable is retrieved at run time, the app crashes with a null pointer exception.
What is the culprit here? Is the drawable image too big? or Is there a limitation in re-sizing the image?
Additionally, is there a limit to the number of files in the res folder or a size limitation in general?
Edit: ok here is some code, of course i'm leaving a lot out but here's the jist:
Here's how I create a handle and give it the correct resource:
Drawable pic;
pic = context.getResources().getDrawable(R.drawable.picture);
Here's how I draw it to the canvas:
pic.setBounds(x, y, x + 100, y + 100);
pic.draw(c);
UPDATE
THIS IS DRIVING ME NUTS!
the problem is clearly an error in java or eclipse, even if it's localized to my machine.
I've done some experimenting and found that the problem could be influenced.
Let's say I have 10 images in my drawable folder. All of the images can be referenced without error except number 7. An example of an error i'm getting is that if I delete number 6, number 7 will display but now number 8 will get the error. This tells me clearly that there aren't errors with the png files themselves, but that the problem is occurring in how they are being retrieved. It also has something to do with the order the images are in.
I've tried everything. clean / build doesn't work. I even re-uploaded the entire drawable file and the error still occurs identically.
It's almost as though there are "holes" in the drawable file that can be filled with pngs, but then they are unusable. and after you fill the holes the files alphabetically after it are not in holes. If you get my meaning.
I've also occasionally run into an odd error where it seems all the sudden every reference retrieves the wrong image. For example: in the example of 10 images referencing 10 would display 9, 9would display 8, 8 would display 7, and so on. This error seems to vanish after a clean and rebuild but I can't help but believe its related.
It turns out that this problem didn't have anything to do with the images themselves, I have not narrowed down the cause completely but through some long, time-consuming troubleshooting I have discovered that the pictures are not the problem, I personally have some other error. I will post a more specific question on the topic later.

Differences between Bitmaps and BufferedImages

I am trying to port some code from a regular Java program into the Android platform. Unfortunately, a significant part of the program involves manipulating images, and Java's AWT was taken away from me. I am trying to replace awt.BufferedImage with Bitmap, and was hoping that the only differences between the two classes would be their interfaces. I read some of the documentation, and it looked like that is true, but after wrapping all of the image stuff into a nice little class and testing almost-the-same code on both my development machine and an actual phone, one program works and the other does not. So:
The encoding for color does not change - right? It is still 0xAARRGGBB - right?
The images themselves are not changed - right? When I put an image into res/drawable, it is exactly the same image. Most notably, they don't alter the resolution in any way - right?
Accessing the pixels is essentially the same - right? I essentially replaced all of my get/setRGB(x,y,RGB) with get/setPixel(x,y,color). There are no changes to the method of indexing into the grid - is there?
Solved: when opening the image, I failed to create a BitmapFactory.Options() with inDither set to false. The BitmapFactory then failed to produce an exact copy, putting some alterations in the resulting Bitmap in order to make it more pleasing for display.

Categories

Resources