启动app应用的时候,在logcat中报这个错误java.lang.NoSuchMethodError: android.ImageView.setBackground 。
解决方法:是版本的问题 Jelly bean(4.1)版本以下 是不支持setBackground 。所以改为setBackgroundDrawable就可以了
activityAttention.setBackgroundDrawable(getResources().getDrawable(
R.drawable.activity_attention_normal));
但是改为setBackgroundDrawable会提示:The method setBackgroundDrawable(Drawable) from the type View is deprecated
解决方法:很明显方法被废弃了,把setBackgroundDrawable 换为setBackgroundResource即可。
且传入的参数直接是resource的id,无需再去通过ID获得View,更加方便。
activityAttention.setBackgroundResource(
R.drawable.activity_attention_selector);
本文解决在Android应用启动时遇到的版本不兼容导致的ImageView背景设置错误,通过调整代码实现兼容不同版本的解决方案,并提供替代方法以避免方法废弃带来的警告。
2270

被折叠的 条评论
为什么被折叠?



