【问题汇总】图片资源引起IllegalArgumentException异常的问题

时间:2022-11-27 00:46:02

在testin平台上执行monkey test会出现错误“IllegalArgumentException“;(三星 GT-S5360)

8.789E/AudioPlayer(1311):mFirstBufferResult=0
07-2217:28:48.898E/AudioPlayer(1311):mFirstBufferResult=0
07-2217:28:49.843E/AndroidRuntime(19219):FATALEXCEPTION:main
07-2217:28:49.843E/AndroidRuntime(19219):java.lang.RuntimeException:UnabletostartactivityComponentInfo{com.zhe800.hongbao/com.zhe800.hongbao.activities.MainActivity}:android.view.InflateException:BinaryXMLfileline#3:Errorinflatingclass
07-2217:28:49.843E/AndroidRuntime(19219):atandroid.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
07-2217:28:49.843E/AndroidRuntime(19219):atandroid.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
07-2217:28:49.843E/AndroidRuntime(19219):atandroid.app.ActivityThread.access$1500(ActivityThread.java:117)
07-2217:28:49.843E/AndroidRuntime(19219):atandroid.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
07-2217:28:49.843E/AndroidRuntime(19219):atandroid.os.Handler.dispatchMessage(Handler.java:99)
07-2217:28:49.843E/AndroidRuntime(19219):atandroid.os.Looper.loop(Looper.java:130)
07-2217:28:49.843E/AndroidRuntime(19219):atandroid.app.ActivityThread.main(ActivityThread.java:3687)
07-2217:28:49.843E/AndroidRuntime(19219):atjava.lang.reflect.Method.invokeNative(NativeMethod)
07-2217:28:49.843E/AndroidRuntime(19219):atjava.lang.reflect.Method.invoke(Method.java:507)
07-2217:28:49.843E/AndroidRuntime(19219):atcom.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
07-2217:28:49.843E/AndroidRuntime(19219):atcom.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
07-2217:28:49.843E/AndroidRuntime(19219):atdalvik.system.NativeStart.main(NativeMethod)
07-2217:28:49.843E/AndroidRuntime(19219):Causedby:android.view.InflateException:BinaryXMLfileline#3:Errorinflatingclass
07-2217:28:49.843E/AndroidRuntime(19219):atandroid.view.LayoutInflater.createView(LayoutInflater.java:518)
07-2217:28:49.843E/AndroidRuntime(19219):atcom.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
07-2217:28:49.843E/AndroidRuntime(19219):atandroid.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568)
07-2217:28:49.843E/AndroidRuntime(19219):atandroid.view.LayoutInflater.parseInclude(LayoutInflater.java:684)
07-2217:28:49.843E/AndroidRuntime(19219):atandroid.view.LayoutInflater.rInflate(LayoutInflater.java:619)
07-2217:28:49.843E/AndroidRuntime(19219):atandroid.view.LayoutInflater.rInflate(LayoutInflater.java:626)
07-2217:28:49.843E/AndroidRuntime(19219):atandroid.view.LayoutInflater.inflate(LayoutInflater.java:408)
07-2217:28:49.843E/AndroidRuntime(19219):atandroid.view.LayoutInflater.inflate(LayoutInflater.java:320)
07-2217:28:49.843E/AndroidRuntime(19219):atandroid.view.LayoutInflater.inflate(LayoutInflater.java:276)
07-2217:28:49.843E/AndroidRuntime(19219):atcom.zhe800.hongbao.fragments.SlidingMenuFragment.onCreateView(SlidingMenuFragment.java:71)
07-2217:28:49.843E/AndroidRuntime(19219):atandroid.support.v4.app.Fragment.performCreateView(Fragment.java:1460)
07-2217:28:49.843E/AndroidRuntime(19219):atandroid.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911)
07-2217:28:49.843E/AndroidRuntime(19219):atandroid.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
07-2217:28:49.843E/AndroidRuntime(19219):atandroid.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
07-2217:28:49.843E/AndroidRuntime(19219):atandroid.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
07-2217:28:49.843E/AndroidRuntime(19219):atandroid.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:551)
07-2217:28:49.843E/AndroidRuntime(19219):atandroid.app.Instrumentation.callActivityOnStart(Instrumentation.java:1129)
07-2217:28:49.843E/AndroidRuntime(19219):atandroid.app.Activity.performStart(Activity.java:3791)
07-2217:28:49.843E/AndroidRuntime(19219):atandroid.app.ActivityThread.performLaunchActivity(ActivityThread.java:1624)
07-2217:28:49.843E/AndroidRuntime(19219):...11more
07-2217:28:49.843E/AndroidRuntime(19219):Causedby:java.lang.reflect.InvocationTargetException
07-2217:28:49.843E/AndroidRuntime(19219):atjava.lang.reflect.Constructor.constructNative(NativeMethod)
07-2217:28:49.843E/AndroidRuntime(19219):atjava.lang.reflect.Constructor.newInstance(Constructor.java:415)
07-2217:28:49.843E/AndroidRuntime(19219):atandroid.view.LayoutInflater.createView(LayoutInflater.java:505)
07-2217:28:49.843E/AndroidRuntime(19219):...29more
07-2217:28:49.843E/AndroidRuntime(19219):Causedby:android.content.res.Resources$NotFoundException:Fileres/drawable-xhdpi/img_menu_divider.pngfromdrawableresourceID#0x7f020078
07-2217:28:49.843E/AndroidRuntime(19219):atandroid.content.res.Resources.loadDrawable(Resources.java:1714)
07-2217:28:49.843E/AndroidRuntime(19219):atandroid.content.res.TypedArray.getDrawable(TypedArray.java:601)
07-2217:28:49.843E/AndroidRuntime(19219):atandroid.widget.ImageView.(ImageView.java:118)
07-2217:28:49.843E/AndroidRuntime(19219):atandroid.widget.ImageView.(ImageView.java:108)
07-2217:28:49.843E/AndroidRuntime(19219):...32more
07-2217:28:49.843E/AndroidRuntime(19219):Causedby:java.lang.IllegalArgumentException:widthandheightmustbe>0
07-2217:28:49.843E/AndroidRuntime(19219):atandroid.graphics.Bitmap.nativeCreate(NativeMethod)
07-2217:28:49.843E/AndroidRuntime(19219):atandroid.graphics.Bitmap.createBitmap(Bitmap.java:477)
07-2217:28:49.843E/AndroidRuntime(19219):atandroid.graphics.Bitmap.createBitmap(Bitmap.java:444)
07-2217:28:49.843E/AndroidRuntime(19219):atandroid.graphics.Bitmap.createScaledBitmap(Bitmap.java:349)
07-2217:28:49.843E/AndroidRuntime(19219):atandroid.graphics.BitmapFactory.finishDecode(BitmapFactory.java:601)
07-2217:28:49.843E/AndroidRuntime(19219):atandroid.graphics.BitmapFactory.decodeStream(BitmapFactory.java:576)
07-2217:28:49.843E/AndroidRuntime(19219):atandroid.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:439)
07-2217:28:49.843E/AndroidRuntime(19219):atandroid.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:697)
07-2217:28:49.843E/AndroidRuntime(19219):atandroid.content.res.Resources.loadDrawable(Resources.java:1709)
07-2217:28:49.843E/AndroidRuntime(19219):...35more
07-2217:28:49.859E/liblog(1481):failedtocalldumpstate
07-2217:28:49.867E/(1481):Dumpstate>/data/log/dumpstate_app_com_zhe800_hongbao_error
07-2217:28:51.375E/StatusBarPolicy(1542):ecio:255
07-2217:28:51.375E/StatusBarPolicy(1542):iconLevel:4
07-2217:28:54.125E/Launcher(8627):setWindowOpaque()
07-2217:28:54.164E/Launcher(8627):MTP-LAUNCHER:mediascanningnotyetfinished.
07-2217:28:54.312E/com.samsung.app(8697):[MSC]>>>StockClock_Integration.java:152[0:0]onReceivesec.android.intent.action.HOME_RESUME
07-2217:28:54.312E/com.samsung.app(8697):[MSC]>>>WidgetIdManager.java:20[0:0]SinaStockClock_WidgetID_Length201
07-2217:28:54.312E/com.samsung.app(8697):[MSC]>>>WidgetIdManager.java:21[0:0]getPrefIDs():length=0
07-2217:28:54.312E/com.samsung.app(8697):[MSC]>>>WidgetIdManager.java:20[0:0]SinaStockClock_WidgetID_Length202
07-2217:28:54.320E/com.samsung.app(8697):[MSC]>>>WidgetIdManager.java:21[0:0]getPrefIDs():length=0
07-2217:28:54.320E/com.samsung.app(8697):[MSC]>>>WidgetIdManager.java:20[0:0]SinaStockClock_WidgetID_Length204
07-2217:28:54.335E/com.samsung.app(8697):[MSC]>>>WidgetIdManager.java:21[0:0]getPrefIDs():length=0
07-2217:28:54.351E/com.samsung.app(8697):[MSC]>>>WidgetIdManager.java:20[0:0]SinaStockClock_WidgetID_Length401
07-2217:28:54.351E/com.samsung.app(8697):[MSC]>>>WidgetIdManager.java:21[0:0]getPrefIDs():length=0
07-2217:28:54.359E/com.samsung.app(8697):[MSC]>>>WidgetIdManager.java:20[0:0]SinaStockClock_WidgetID_Length402
07-2217:28:54.359E/com.samsung.app(8697):[MSC]>>>WidgetIdManager.java:21[0:0]getPrefIDs():length=0
07-2217:28:54.390E/com.samsung.app(8697):[MSC]>>>WidgetIdManager.java:20[0:0]SinaStockClock_WidgetID_Length404
07-2217:28:54.390E/com.samsung.app(8697):[MSC]>>>WidgetIdManager.java:21[0:0]getPrefIDs():length=0
07-2217:28:54.390E/com.samsung.app(8697):[MSC]>>>WidgetIdManager.java:20[0:0]SinaStockClock_WidgetID_Length411
07-2217:28:54.390E/com.samsung.app(8697):[MSC]>>>WidgetIdManager.java:21[0:0]getPrefIDs():length=0
07-2217:28:54.390E/com.samsung.app(8697):[MSC]>>>WidgetIdManager.java:20[0:0]SinaStockClock_WidgetID_Length414
07-2217:28:54.398E/com.samsung.app(8697):[MSC]>>>WidgetIdManager.java:21[0:0]getPrefIDs():length=0
07-2217:28:56.031E/AndroidRuntime(19371):/system/csc/feature.xml==>cannotopenfile
07-2217:28:56.617E/AndroidRuntime(19381):/system/csc/feature.xml==>cannotopenfile
跟踪错误日志,问题被定位到如下过程:

07-2217:28:49.843E/AndroidRuntime(19219):Causedby:android.content.res.Resources$NotFoundException:Fileres/drawable-xhdpi/img_menu_divider.pngfromdrawableresourceID#0x7f02007807-2217:28:49.843E/AndroidRuntime(19219):atandroid.content.res.Resources.loadDrawable(Resources.java:1714)07-2217:28:49.843E/AndroidRuntime(19219):atandroid.content.res.TypedArray.getDrawable(TypedArray.java:601)07-2217:28:49.843E/AndroidRuntime(19219):atandroid.widget.ImageView.(ImageView.java:118)07-2217:28:49.843E/AndroidRuntime(19219):atandroid.widget.ImageView.(ImageView.java:108)07-2217:28:49.843E/AndroidRuntime(19219):...32more07-2217:28:49.843E/AndroidRuntime(19219):Causedby:java.lang.IllegalArgumentException:widthandheightmustbe>007-2217:28:49.843E/AndroidRuntime(19219):atandroid.graphics.Bitmap.nativeCreate(NativeMethod)07-2217:28:49.843E/AndroidRuntime(19219):atandroid.graphics.Bitmap.createBitmap(Bitmap.java:477)07-2217:28:49.843E/AndroidRuntime(19219):atandroid.graphics.Bitmap.createBitmap(Bitmap.java:444)07-2217:28:49.843E/AndroidRuntime(19219):atandroid.graphics.Bitmap.createScaledBitmap(Bitmap.java:349)07-2217:28:49.843E/AndroidRuntime(19219):atandroid.graphics.BitmapFactory.finishDecode(BitmapFactory.java:601)07-2217:28:49.843E/AndroidRuntime(19219):atandroid.graphics.BitmapFactory.decodeStream(BitmapFactory.java:576)07-2217:28:49.843E/AndroidRuntime(19219):atandroid.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:439)07-2217:28:49.843E/AndroidRuntime(19219):atandroid.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:697)07-2217:28:49.843E/AndroidRuntime(19219):atandroid.content.res.Resources.loadDrawable(Resources.java:1709)

根据日志,错误是由于img_menu_divider.png这张图片没有找到引起的。再仔细查看,发现是由于图片的宽和高小于0,导致java.lang.IllegalArgumentException:widthandheightmustbe>0。

猜测是由于这张图片存在某些问题,导致android framework无法加载。


最后,采用色值代替图片资源,问题得以解决。

<ImageView xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="0.5dp"    android:paddingTop="0.0dp"    android:paddingBottom="0.0dp"    android:layout_marginLeft="20dp"    android:background="#ffb297" ></ImageView>