造成原因:View.OnClickListener是一个内部匿名类。在内部匿名类中调用外部类元素需要final
解决办法:
class FollowOnClickListener implements ImageView.OnClickListener { private ImageView iconImageView; private TextView nameTextView; public FollowOnClickListener(ImageView iconIV, TextView nameTV) { iconImageView = iconIV; nameTextView = nameTV; } public void onClick(View v) { if (!mFollowFile.exists()) { mHandler.sendEmptyMessage(12); return; } flag[location] = !flag[location]; if (flag[location]) { ((ImageView) v).setImageResource(R.drawable.unfollow_icon); // 取消关注 置灰 nameTextView.setTextColor(Color.parseColor(Constants.COLOR_LIGHT_GRAY)); iconImageView.setImageBitmap(BitmapUtils .toGrayscale(contact.getContactPhotoBitmap())); } else { ((ImageView) v).setImageResource(R.drawable.follow_icon); nameTextView.setTextColor(Color.parseColor(Constants.COLOR_BLACK)); iconImageView.setImageBitmap(contact .getContactPhotoBitmap()); } Message msg = new Message(); msg.what = 7; msg.arg1 = location; mHandler.sendMessage(msg); } } viewHolder.unFollowImageView .setOnClickListener(new FollowOnClickListener( viewHolder.iconImageView, viewHolder.nameFollowTextView));
http://zhidao.baidu.com/question/289905031.html