如何解决Android出现的异常“java.lang.NoSuchMethodError: android.widget.LinearLayout.setBackground”?
16lz
2021-01-23
问题描述
部分Android手机打开app报错,调试不报错,运行时报错。
原因
setBackground是Android4.1(API16)之后才提供的方法,所以4.1之前的系统会出现这个异常。
已解决
使用setBackgroundDrawable方法(android建议弃用),不过,最好还是用setBackgroundResource方法设置控件背景。
Drawable d = getResources().getDrawable(R.drawable.ic_launcher);Button one = new Button(this);// mediocreone.setBackgroundDrawable(d);Button two = new Button(this);// bettertwo.setBackgroundResource(R.drawable.ic_launcher);
参考
http://stackoverflow.com/questions/18559248/button-setbackgrounddrawable-background-throws-nosuchmethoderror
为什么开发模式下不报错,运行时报错呢?
原因: 新老版本并存,而老版本的jar包路径还在classpath中,并且在 新版本的前面,程序调用新方法,编译没问题,但运行时会先去找旧的classes,而旧的classes中不含此方法,故报 NoSuchMethodError错误,而且这个还不是异常,捕获不了,不好找原因。
参考:http://www.apkbus.com/android-144758-1-1.html
更多相关文章
- Android 2.3.3 安卓系统 源代码 在Eclipse查看方法
- mtk android内置nginx服务器的方法
- ubuntu 编译 Android 出现的若干错误及解决方法
- 解决EventBus中接收方法中无法更新UI的问题
- Android保持屏幕常亮的三种方法
- Android .so abi兼容,通用armeabi-v7a和arm64-v8a架构的方法
- android sdcard读取时报错PVMFErrNotSupported