Weird FileNotFoundException exception from Android

2020-04-04 17:03发布

I'm working on an Android app which is now in production, and am occasionally seeing exceptions (reported via airbrake) with stuff like this:

[1.0.4] java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mycompany.android/com.mycompany.android.activities.MainActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class <unknown>
... cut lots of stuff ...
### CAUSED BY ###: java.io.FileNotFoundException: res/drawable-hdpi/tab_active.9.png:
AssetManager.java:-2:in `android.content.res.AssetManager.openNonAssetNative'
AssetManager.java:406:in `android.content.res.AssetManager.openNonAsset'
Resources.java:1706:in `android.content.res.Resources.loadDrawable'
... cut lots more stuff ...

In testing, this view definitely worked on multiple HDPI devices, so that resource was fine there. Before building the final APK, I did a full clean/rebuild, and of course sent the APK out for internal testing. The fact that I get 1-2 exceptions like this per version instead of 10,000 would indicate that this resource is definitely packaged correctly for most users.

I'm completely stumped and unsure why it wouldn't load on certain phones. Has anyone seen something like this in their apps?

标签: android
2条回答
狗以群分
2楼-- · 2020-04-04 17:20

I never did track down the exact cause of this problem, but based on my experience developing the app in question I believe it was caused by hitting the memory limit for the activity. After your activity runs out of memory, it seems that all sorts of strange errors can happen, including stuff like this.

When I made other improvements to the app's memory management (particularly by using SoftReference with Bitmaps), these type of exceptions starting appearing less frequently.

查看更多
混吃等死
3楼-- · 2020-04-04 17:36

I guess, this devices are running Android 1.5

From the Docs on "Providing the Best Device Compatibility with Resources":

Providing default resources is important not only because your application might run on a configuration you had not anticipated, but also because new versions of Android sometimes add configuration qualifiers that older versions do not support. If you use a new resource qualifier, but maintain code compatibility with older versions of Android, then when an older version of Android runs your application, it will crash if you do not provide default resources, because it cannot use the resources named with the new qualifier. For example, if your minSdkVersion is set to 4, and you qualify all of your drawable resources using night mode (night or notnight, which were added in API Level 8), then an API level 4 device cannot access your drawable resources and will crash. In this case, you probably want notnight to be your default resources, so you should exclude that qualifier so your drawable resources are in either drawable/ or drawable-night/.

Since you are using the -hdpi-qualifier, this might be the problem because those where introduced in Android 1.6 (API-Level 4) and are therefore not available under Android 1.5

See this topic: Drawable-hdpi, Drawable-mdpi, Drawable-ldpi Android

So, providing some fallback-drawbale resources in the res/drawable-folder might solve your issue.


There seams to be a Bug based on my original idea which is described here: NotFoundException and FileNotFoundException when running app on Android 1.5

It seams that the error is thrown for the next resource. So be sure to provide fallback's for every drawable in the res/drawable-folder.

This addresses Android 1.5, I'm not sure if this is still a problem in Android 2.3, but it seams that different manufactures handle those things different.

查看更多
登录 后发表回答