android代码中添加bug,Android开发中的BUG列表

1.java.lang.NoSuchMethodError: android.content.res.Resources.getDrawable

或者 java.lang.NoSuchMethodError:android.content.Context.getDrawable

原因:Context类的getDrawable(res)方法和Resources的getDrawable(res,theme)都是API21才添加的,低版本系统无法找到该方法所以报异常。

解决办法:

使用Resources的getDrawable(res),但是该方法在API22已废弃。

使用ContextCompat.getDrawable(context,res)。

2.java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive a notification. Make sure the content of your adapter is not modified from a background thread, but only from the UI thread.

原因:该异常在ListView的layoutChildren()方法中抛出,代码如下:

if (mItemCount == 0) {

resetList();

invokeOnItemScrollListener();

return;

} else if (mItemCount != mAdapter.getCount()) {

throw new IllegalStateException("The content of the adapter has changed but " + "ListView did not receive a notification. Make sure the content of " + "your adapter is not modified from a background thread, but only from " + "the UI thread. Make sure your adapter calls notifyDataSetChanged() " + "when its content changes. [in ListView(" + getId() + ", " + getClass() + ") with Adapter(" + mAdapter.getClass() + ")]");

}

即当Adapter的数据改变时,却未及时通知到ListView就会抛出这个异常。一种是Adapter的数据改变了却没有调用notifyDataSetChanged(),一种是改变Adapter数据在其它线程,调用notifyDataSetChanged()在主线程,之间有延迟。

解决办法:

确保改变数据完立即通知ListView。

如果Adapter的数据处理在其它线程,那就让Adapter持有一份数据,其它线程处理完数据后用Handler发通知,在UI线程中改变Adapter的数据和通知ListView。

3.UnsupportedMethodException

Unsupported method:InstantRun.getRestartDexFile().The version of Gradle you connect to does not support that method.

原因:Android Studio的版本和Gradle版本不兼容。

解决办法:升级AS或者更改Gradle的版本。

4.java.lang.NoSuchMethodError: android.graphics.drawable.Drawable.getAlpha

原因:Drawable.getAlpha是API 19才添加的。

Drawable.setAlpha是API 1就可以用,这种不对称真是坑啊。

注意View.setAlpha和View.getAlpha都是API 11添加的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值