NotFoundException and FileNotFoundException when running app on Android 1.5
I'm trying to backport an android 1.6+ application to android 1.5.
Following the advice here:
- http://comments.gmane.org/gmane.comp.handhelds.android.devel/97051
- http://groups.google.com/group/android-developers/browse_thread/thread/ff22f6e42a4a46d2/4201a20aaa23069d
- Android 2.2 SDK breaks compatibility with older phones
- https://developer.android.com/guide/topics/resources/providing-resources.html#ScreenCompatibility
... I have done the following:
- Modify AndroidManifest.xml to set minSdkVersion to 3
- Move all of my files that were previously in drawable-mdpi/ to drawable/
- Rename drawable-hdpi/ to drawable-hdpi-v4/
It seems to me that this should ensure that 1.5 devices use the files in drawable/ while 1.6 and later devices use the files in drawable/ and drawable-hdpi-v4/ as appropriate. 开发者_运维问答The drawable/ and drawable-hdpi-v4/ directories are the only drawable directories in my res folder.
However, after I compile, install, and run the resulting binary on the 1.5 emulator, I get the following error:
E/AndroidRuntime( 1096): Caused by: java.lang.reflect.InvocationTargetException
E/AndroidRuntime( 1096): at android.widget.ImageView.<init>(ImageView.java:103)
E/AndroidRuntime( 1096): at java.lang.reflect.Constructor.constructNative(Native Method)
E/AndroidRuntime( 1096): at java.lang.reflect.Constructor.newInstance(Constructor.java:446)
E/AndroidRuntime( 1096): at android.view.LayoutInflater.createView(LayoutInflater.java:499)
E/AndroidRuntime( 1096): ... 26 more
E/AndroidRuntime( 1096): Caused by: android.content.res.Resources$NotFoundException: File res/drawable/bg.png from drawable resource ID #0x7f02002e
E/AndroidRuntime( 1096): at android.content.res.Resources.loadDrawable(Resources.java:1641)
E/AndroidRuntime( 1096): at android.content.res.TypedArray.getDrawable(TypedArray.java:548)
E/AndroidRuntime( 1096): at android.widget.ImageView.<init>(ImageView.java:113)
E/AndroidRuntime( 1096): ... 30 more
E/AndroidRuntime( 1096): Caused by: java.io.FileNotFoundException: res/drawable/bg.png
E/AndroidRuntime( 1096): at android.content.res.AssetManager.openNonAssetNative(Native Method)
E/AndroidRuntime( 1096): at android.content.res.AssetManager.openNonAsset(AssetManager.java:392)
E/AndroidRuntime( 1096): at android.content.res.Resources.loadDrawable(Resources.java:1634)
E/AndroidRuntime( 1096): ... 32 more
For reasons I don't understand, 1.5 devices are not able to see the bg.png image file, a version of which is in both the drawable/ and drawable-hdpi-v4/ directories.
Running the same binary works fine on 1.6.
Why won't 1.5 devices see my res/drawable/bg.png image with this setup?
UPDATE: As described in Providing screen resource compatibility for Android 1.5, I'm using Android SDK r6 and have put my mdpi resources in the drawable/ directory. Also, I've verified that the problem isn't isolated to bg.png. If I remove the reference to the broken drawable in my xml, the app breaks on the next and each subsequent graphic during setContentView().
Success!!!
The problem was that I had one foo.png resource in drawable-hdpi-v4/ that was not present in the drawable/ directory. When I referenced R.drawable.foo from my layout file, the 1.5 emulator was therefore not able to find a suitable resource for that id. What made it so difficult to track down was that the error didn't happen immediately when referencing foo.png, but rather the error was thrown on the NEXT resource referenced, which was bg.png. Google has confirmed this is a bug.
Adding an mdpi version of foo.png to the drawable/ directory fixed the problem.
If you are building your project using Android 1.6 with minSDKVesrion set to 3, you can create only 3 folders drawable-hdpi,drawable-mdpi,drawable-ldpi. I recreated your issue in my project and when I changed the Res folder structure it started working perfectly. You can surely give it a try (There is a Android 1.5 issue you be aware of. Many 1.5 mdpi devices pick up resources from drawable-ldpi folder.)
I faced the same problem a few days back. And the only solution I found was to create a new project from scratch and add you source files and res files to them. This would definitely fix the problem.
精彩评论