解决Android1.5版本加载图片出错的问题
前提是这个错误是由于我个人原因所造成,可能大部分人不会遇到这个错误。
我的错误现象是这样的:
我是先建立一个2.2版本的android project ,做好一个例子在模拟器上运行好,没有问题,然后我找到一个android真机(HTC hero G3),但sdk是1.5版本的,
所以我就将项目引用的android sdk 版本改成1.5,然后我再运行,就出现如下错误:
05-03 14:36:00.268: WARN/ResourceType(776): Failure getting entry for 0x7f020006 (t=1 e=6) in package 0: 0xffffffb5 05-03 14:36:09.697: WARN/ActivityManager(542): Launch timeout has expired, giving up wake lock! 05-03 14:36:09.978: WARN/ActivityManager(542): Activity idle timeout for HistoryRecord{436cec90 {com.kjsoft.oa/com.kjsoft.oa.Notice}} 05-03 14:36:44.320: DEBUG/AndroidRuntime(776): Shutting down VM 05-03 14:36:44.320: WARN/dalvikvm(776): threadid=3: thread exiting with uncaught exception (group=0x4000fe70) 05-03 14:36:44.329: ERROR/AndroidRuntime(776): Uncaught handler: thread main exiting due to uncaught exception 05-03 14:36:44.689: ERROR/AndroidRuntime(776): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.kjsoft.oa/com.kjsoft.oa.Notice}: android.view.InflateException: Binary XML file line #8: Error inflating class java.lang.reflect.Constructor 05-03 14:36:44.689: ERROR/AndroidRuntime(776): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2268) 05-03 14:36:44.689: ERROR/AndroidRuntime(776): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2284) 05-03 14:36:44.689: ERROR/AndroidRuntime(776): at android.app.ActivityThread.access$1800(ActivityThread.java:112) 05-03 14:36:44.689: ERROR/AndroidRuntime(776): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1692) 05-03 14:36:44.689: ERROR/AndroidRuntime(776): at android.os.Handler.dispatchMessage(Handler.java:99) 05-03 14:36:44.689: ERROR/AndroidRuntime(776): at android.os.Looper.loop(Looper.java:123) 05-03 14:36:44.689: ERROR/AndroidRuntime(776): at android.app.ActivityThread.main(ActivityThread.java:3948) 05-03 14:36:44.689: ERROR/AndroidRuntime(776): at java.lang.reflect.Method.invokeNative(Native Method) 05-03 14:36:44.689: ERROR/AndroidRuntime(776): at java.lang.reflect.Method.invoke(Method.java:521) 05-03 14:36:44.689: ERROR/AndroidRuntime(776): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782) 05-03 14:36:44.689: ERROR/AndroidRuntime(776): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540) 05-03 14:36:44.689: ERROR/AndroidRuntime(776): at dalvik.system.NativeStart.main(Native Method) 05-03 14:36:44.689: ERROR/AndroidRuntime(776): Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class java.lang.reflect.Constructor 05-03 14:36:44.689: ERROR/AndroidRuntime(776): at android.view.LayoutInflater.createView(LayoutInflater.java:512) 05-03 14:36:44.689: ERROR/AndroidRuntime(776): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56) 05-03 14:36:44.689: ERROR/AndroidRuntime(776): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:562) 05-03 14:36:44.689: ERROR/AndroidRuntime(776): at android.view.LayoutInflater.rInflate(LayoutInflater.java:617) 05-03 14:36:44.689: ERROR/AndroidRuntime(776): at android.view.LayoutInflater.rInflate(LayoutInflater.java:620) 05-03 14:36:44.689: ERROR/AndroidRuntime(776): at android.view.LayoutInflater.inflate(LayoutInflater.java:407) 05-03 14:36:44.689: ERROR/AndroidRuntime(776): at android.view.LayoutInflater.inflate(LayoutInflater.java:320) 05-03 14:36:44.689: ERROR/AndroidRuntime(776): at android.view.LayoutInflater.inflate(LayoutInflater.java:276) 05-03 14:36:44.689: ERROR/AndroidRuntime(776): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:309) 05-03 14:36:44.689: ERROR/AndroidRuntime(776): at android.app.Activity.setContentView(Activity.java:1626) 05-03 14:36:44.689: ERROR/AndroidRuntime(776): at com.kjsoft.oa.Notice.onCreate(Notice.java:33) 05-03 14:36:44.689: ERROR/AndroidRuntime(776): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123) 05-03 14:36:44.689: ERROR/AndroidRuntime(776): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2231) 05-03 14:36:44.689: ERROR/AndroidRuntime(776): ... 11 more 05-03 14:36:44.689: ERROR/AndroidRuntime(776): Caused by: java.lang.reflect.InvocationTargetException 05-03 14:36:44.689: ERROR/AndroidRuntime(776): at android.widget.ImageView.<init>(ImageView.java:103) 05-03 14:36:44.689: ERROR/AndroidRuntime(776): at java.lang.reflect.Constructor.constructNative(Native Method) 05-03 14:36:44.689: ERROR/AndroidRuntime(776): at java.lang.reflect.Constructor.newInstance(Constructor.java:446) 05-03 14:36:44.689: ERROR/AndroidRuntime(776): at android.view.LayoutInflater.createView(LayoutInflater.java:499) 05-03 14:36:44.689: ERROR/AndroidRuntime(776): ... 23 more 05-03 14:36:44.689: ERROR/AndroidRuntime(776): Caused by: android.content.res.Resources$NotFoundException: File res/color/primary_text_light.xml from drawable resource ID #0x7f020006 05-03 14:36:44.689: ERROR/AndroidRuntime(776): at android.content.res.Resources.loadDrawable(Resources.java:1625) 05-03 14:36:44.689: ERROR/AndroidRuntime(776): at android.content.res.TypedArray.getDrawable(TypedArray.java:548) 05-03 14:36:44.689: ERROR/AndroidRuntime(776): at android.widget.ImageView.<init>(ImageView.java:113) 05-03 14:36:44.689: ERROR/AndroidRuntime(776): ... 27 more 05-03 14:36:44.689: ERROR/AndroidRuntime(776): Caused by: android.content.res.Resources$NotFoundException: File res/color/primary_text_light.xml from xml type drawable resource ID #0x7f020006 05-03 14:36:44.689: ERROR/AndroidRuntime(776): at android.content.res.Resources.loadXmlResourceParser(Resources.java:1843) 05-03 14:36:44.689: ERROR/AndroidRuntime(776): at android.content.res.Resources.loadDrawable(Resources.java:1620) 05-03 14:36:44.689: ERROR/AndroidRuntime(776): ... 29 more 05-03 14:36:44.689: ERROR/AndroidRuntime(776): Caused by: java.io.FileNotFoundException: res/color/primary_text_light.xml 05-03 14:36:44.689: ERROR/AndroidRuntime(776): at android.content.res.AssetManager.openXmlAssetNative(Native Method) 05-03 14:36:44.689: ERROR/AndroidRuntime(776): at android.content.res.AssetManager.openXmlBlockAsset(AssetManager.java:471) 05-03 14:36:44.689: ERROR/AndroidRuntime(776): at android.content.res.Resources.loadXmlResourceParser(Resources.java:1825) 05-03 14:36:44.689: ERROR/AndroidRuntime(776): ... 30 more
这个问题困扰了我好几天,我运行1.5版本的apiDemos也正常运行,就是找不到原因。
经过无数次的尝试后发现,我把项目中的加载图片的地方都删除之后,就可以正常运行了,加上之后又不行了,觉得很奇怪。
最终发现原来是图片路径的问题,原因如下:
我基于2.2版本的sdk建立的项目中,res目录下的结构如下图所示:
系统默认将图片分成三个文件夹,目的是根据不同的屏幕分辨率显示不同的图片,但是我发现sdk1.5的项目中,却只有名称为 drawable 的这一个文件夹。
所以我在 res 下新建了 drawable 文件夹,将图片全部挪到这个文件夹中,重新运行,终于运行成功!