【项目笔记】【bug】数组空指针异常
1 package com.example.googleplay.ui.holder; 2 3 import java.util.ArrayList; 4 5 import android.view.View; 6 import android.widget.ImageView; 7 8 import com.example.googleplay.R; 9 import com.example.googleplay.http.HttpHelper; 10 import com.example.googleplay.utils.BitmapHelper; 11 import com.example.googleplay.utils.UIUtils; 12 import com.lidroid.xutils.BitmapUtils; 13 14 public class HomeScreenHolder extends BaseHolder<ArrayList<String>> { 15 16 private ImageView[] mImages = null; 17 private BitmapUtils mBitmapUtils; 18 19 @Override 20 public View initView() { 21 View view = UIUtils.inflate(R.layout.layout_detail_picinfo); 22 23 mImages = new ImageView[5]; 24 mImages[0] = (ImageView) view.findViewById(R.id.iv_pic1); 25 mImages[1] = (ImageView) view.findViewById(R.id.iv_pic2); 26 mImages[2] = (ImageView) view.findViewById(R.id.iv_pic3); 27 mImages[3] = (ImageView) view.findViewById(R.id.iv_pic4); 28 mImages[4] = (ImageView) view.findViewById(R.id.iv_pic5); 29 30 mBitmapUtils = BitmapHelper.getBitmapUtils(); 31 return view; 32 } 33 34 @Override 35 public void refreshView(ArrayList<String> data) { 36 37 for (int i = 0; i < mImages.length; i++) { 38 if(i>=data.size()){ 39 //多余的图片布局需要隐藏 40 mImages[i].setVisibility(View.GONE); 41 }else { 42 //要显示的图片 43 mImages[i].setVisibility(View.VISIBLE); 44 mBitmapUtils.display(mImages[i], HttpHelper.URL + "image?name=" + data.get(i)); 45 } 46 } 47 } 48 }
在refreshView这个方法中mImages报空指针异常,其实是第16行的原因,数组赋值只能一次赋值,赋值之后再赋值无效,应该写成
1 private ImageView[] mImages;
但是可以给数组的每个元素再次赋值。