安卓编程的各种坑

持续更新------------------

回调函数是protected权限,重写时还是protected导致的错误,解决办法是用public;比如Activity的onDestroy()函数;

同样的函数用support-vX包中的,导致错误,解决办法不用support-vX包中的函数,比如SearchView;


资源回收导致的问题如:在onCreate()中有

bitmap = BitmapFactory.decodeResource(this.getResources(), R.drawable.flower);

imageView.setImageBitmap(bitmap);[ 1]

回调函数中:

BitmapDrawable tmp = (BitmapDrawable)
imageView.getDrawable();
if (!tmp.getBitmap().isRecycled())  
{
tmp.getBitmap().recycle();
}

Bitmap bitmap2 = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
imageView.setImageBitmap(bitmap2);

这时就会出错;bitmap别强制回收了;

解决办法只要保证bitmap不被回收即可;[ 1]处改为

bitmap = BitmapFactory.decodeResource(this.getResources(), R.drawable.flower);

bitmap 1= BitmapFactory.decodeResource(this.getResources(), R.drawable.flower);

imageView.setImageBitmap(bitmap1);这样bitmap1会被回收而bitmap不会被回收;


函数/this使用位置不对的问题:

WallpaperManager.getInstance(this);如果放在类的初始块位置,会导致程序崩溃;

因为初始化块的执行早于构造器的执行。也就是说,如果该函数放在初始化块的话,this还没有

指向实例,它是空指针;同样的如果你在初始化块执行了一个实例方法也会导致程序崩溃;


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值