volution
volution

Reputation: 533

Weird problem about an android drawable

I have a view i dynamically assign background to using SetBackgroundResource(R.drawable.led_red). This was working fine for weeks but suddenly it stopped working for one of the images i'm using for this purpose (led_blue) even though i didn't change anything in the project related to those images. Image simply doesn't appear even though same code works fine with other variations of the led image. This problematic color was also working before...

I tried a lot of things, it seems like if i add another image to the project so that id (0x7f020000) of that problematic image in R.java changes to something else, it starts working again. But of course i cannot simply solve the problem with that because unless i found the reason why this happened, it may happen for other images in the future.

Also new image i added which uses 0x7f020000 now is also not working so it seems like it is related to this individual id but i couldn't make sense of it.

This happens both on device and on the simulator using 2.1 SDK

Any ideas what is going on?

Thanks.

Upvotes: 3

Views: 1383

Answers (2)

Christopher Orr
Christopher Orr

Reputation: 111555

Short answer:
It's a bug and is fixed in Android 3.0 and newer.


Long answer:
I have also encountered this bug several times over the past few years, though at one point I finally dug into the source code and discovered it occurs due to a low-level resource cache collision bug.

Thankfully, this was fixed around the same time by AOSP change Ia971bb242ceac5e8f9346094009a10f356399ab9, which was reviewed and merged in July 2010.

However, according to the platform/frameworks/base git repository, this wasn't released until Honeycomb, in February 2011.

In September 2011, with the bug still existent in the majority of released devices, this was filed as Android bug 20283.

The workaround to support earlier devices is to add an empty <resources> file which comes first alphabetically (e.g. "a_workaround.xml"), thereby increasing the resource ID of the other drawables. As mentioned in other answers, an empty image works too.

Upvotes: 2

vovkab
vovkab

Reputation: 1292

Try to add empty(1pixel_transparent) png file, name it a.png, so it will be first with id 0x7f020000.

Upvotes: 1

Related Questions