1、selector.xml与图片重名。
java.lang.StackOverflowError
at android.graphics.Bitmap.getScaledWidth(Bitmap.java:1138)
at android.graphics.drawable.BitmapDrawable.computeBitmapSize(BitmapDrawable.java:189)
at android.graphics.drawable.BitmapDrawable.setBitmap(BitmapDrawable.java:197)
at android.graphics.drawable.BitmapDrawable.<init>(BitmapDrawable.java:700)
at android.graphics.drawable.BitmapDrawable.<init>(BitmapDrawable.java:62)
at android.graphics.drawable.BitmapDrawable$BitmapState.newDrawable(BitmapDrawable.java:684)
at android.content.res.Resources.getCachedDrawable(Resources.java:2251)
at android.content.res.Resources.loadDrawable(Resources.java:2112)
at com.huawei.android.content.res.ResourcesEx.loadDrawable(ResourcesEx.java:640)
at android.content.res.Resources.getDrawable(Resources.java:747)
at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:176)
at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:937)
at android.graphics.drawable.Drawable.createFromXml(Drawable.java:877)
at android.content.res.Resources.loadDrawable(Resources.java:2156)
at com.huawei.android.content.res.ResourcesEx.loadDrawable(ResourcesEx.java:640)
at android.content.res.Resources.getDrawable(Resources.java:747)
at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:176)
at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:937)
at android.graphics.drawable.Drawable.createFromXml(Drawable.java:877)
at android.content.res.Resources.loadDrawable(Resources.java:2156)
at com.huawei.android.content.res.ResourcesEx.loadDrawable(ResourcesEx.java:640)
at android.content.res.Resources.getDrawable(Resources.java:747)
at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:176)
at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:937)
at android.graphics.drawable.Drawable.createFromXml(Drawable.java:877)
at android.content.res.Resources.loadDrawable(Resources.java:2156)
at com.huawei.android.content.res.ResourcesEx.loadDrawable(ResourcesEx.java:640)
at android.content.res.Resources.getDrawable(Resources.java:747)
at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:176)
at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:937)
at android.graphics.drawable.Drawable.createFromXml(Drawable.java:877)
at android.content.res.Resources.loadDrawable(Resources.java:2156)
有次遇到了这个问题。看到日志的时候一脸懵逼,而且bug是出现在一台很奇怪的设备中,正常设备没有问题。
后来我们分析发现,下面的日志是重复的,这里栈溢出,其实是因为循环加载了bitmap。
后来我们发现,是drawable里的xml和图片文件重名了。在作为selector的xml中,调用了图片文件,然后因为我们项目中只支持 xh和xxh的drawable,因此,在dpi低的设备中会导致selector中的调用无法匹配到图片文件,然后匹配到了 drawable中的xml本身,然后就进入了循环匹配,最终导致栈溢出。
2、SharePreference 改名与变量类型改变的版本更替出错
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.inpor.fastmeetingcloud/com.inpor.fastmeetingcloud.activity.MeetingActivity}: java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2504)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2576)
at android.app.ActivityThread.access$1000(ActivityThread.java:163)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1461)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:159)
at android.app.ActivityThread.main(ActivityThread.java:5541)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:975)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:770)
Caused by: java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long
at android.app.SharedPreferencesImpl.getLong(SharedPreferencesImpl.java:273)
at com.inpor.manager.config.ConfigService.LoadConfig(ConfigService.java:25)
at com.inpor.manager.model.CameraDeviceController.getVideoParam(CameraDeviceController.java:402)
at com.inpor.manager.model.CameraDeviceController.openCamera(CameraDeviceController.java:255)
at com.inpor.fastmeetingcloud.presenter.VideoController.<init>(VideoController.java:64)
at com.inpor.fastmeetingcloud.activity.MeetingActivity.initViews(MeetingActivity.java:240)
at com.inpor.fastmeetingcloud.activity.MeetingActivity.onCreate(MeetingActivity.java:198)
at android.app.Activity.performCreate(Activity.java:6163)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2457)
... 10 more
java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long
at android.app.SharedPreferencesImpl.getLong(SharedPreferencesImpl.java:273)
at com.inpor.manager.config.ConfigService.LoadConfig(ConfigService.java:25)
at com.inpor.manager.model.CameraDeviceController.getVideoParam(CameraDeviceController.java:402)
at com.inpor.manager.model.CameraDeviceController.openCamera(CameraDeviceController.java:255)
at com.inpor.fastmeetingcloud.presenter.VideoController.<init>(VideoController.java:64)
at com.inpor.fastmeetingcloud.activity.MeetingActivity.initViews(MeetingActivity.java:240)
at com.inpor.fastmeetingcloud.activity.MeetingActivity.onCreate(MeetingActivity.java:198)
at android.app.Activity.performCreate(Activity.java:6163)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2457)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2576)
at android.app.ActivityThread.access$1000(ActivityThread.java:163)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1461)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:159)
at android.app.ActivityThread.main(ActivityThread.java:5541)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:975)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:770)
新版本上线后,有一个崩溃一天崩了一百多次。我们都很奇怪,明明在SharePreference中这个变量是long,怎么会出错呢?
原来,发生了一件很巧合的事情。
在一个比较久远的版本1中 存储参数的 SharePreference 被起名A, 里面有一个 int的参数被命名为 a。后来因为int位数不够用,将a 从 int 变为了 long,同时,为了防止获取失败, SharePreference 被改名为 B,这个改变一直被延续到 上个版本2。在此次发布的新版本3中,因为又有东西被修改了,因此按照惯例,我们将SharePreference改名。但是好巧不巧,我们为B改的新名字,就是A。因此,从版本1就开始使用,并且一直是通过覆盖安装来进行版本升级的用户,就会一直保存着版本1的SharePreference,其参数a仍然是int,导致在版本3中因为名字改成了A,又读取了版本1中保留的数据,导致读取失败。