我在做一个拼图游戏,现在想当用户点击了某个小图片后,标记为高亮(或则其他变化也可以),点击另外一个后图片交换然后高亮消失。
以下是我的实现代码,可是报错为空指针异常。
public final float[] BT_SELECTED = new float[] {1,0,0,0,50,0,1,0,0,50,0,0,1,0,50,0,0,0,1,0};
private OnClickListener OnClickImageView1 = new OnClickListener() {
@Override
public void onClick(View v) {
if (clickNum==0) {
for (int i = 0; i < mImages.length; i++) {
boolean f = false;
for (int j = 0; j < mImages[i].length; j++) {
ImageView imgg=mImages[i][j];
imgg.getBackground().setColorFilter(new ColorMatrixColorFilter(BT_SELECTED));
imgg.setBackgroundDrawable(imgg.getBackground());//此处报错为空指针
if (imgg==v) {
x=i;
y=j;
clickNum++;
f=true;
break;
}
}
if(f){
break;
}
}
}else {
for (int i = 0; i < mImages.length; i++) {
for (int j = 0; j < mImages[i].length; j++) {
ImageView imgg=mImages[i][j];
if (imgg==v) {
if (clickNum==1) {
changePosition(i,j,x,y);
x=0;
y=0;
clickNum=0;
}
}
}
}
}
}
};
------解决方案--------------------
pageImageView.setBackgroundDrawable(pageImageView.getDrawable());