基本上我有一个sqlite数据库,允许我插入带有标题的酒店图像。以前我有一个问题,因为我在列表中显示了重复的图像,但感谢上帝,我修复了它。现在图像显示得很好。但是,当我向下或向上滚动列表视图时,应用程序崩溃了。
以下是我初始化自定义适配器的方法。
adapter = new HotelArrayAdapter(this, R.layout.single_row,
imageArry);
ListView dataList = (ListView) findViewById(R.id.list);
dataList.setAdapter(adapter);
这就是我如何设计它覆盖getView()方法。
public class HotelArrayAdapter extends ArrayAdapter<Hotels> {
Context context;
int layoutResourceId;
ArrayList<Hotels> data = new ArrayList<Hotels>();
public HotelArrayAdapter(Context context, int layoutResourceId,
ArrayList<Hotels> data) {
super(context, layoutResourceId, data);
// TODO Auto-generated constructor stub
this.layoutResourceId=layoutResourceId;
this.context = context;
this.data = data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View row = convertView;
ImageHolder holder = null;
if(row==null){
LayoutInflater inflater =(LayoutInflater)
((Activity)context).getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(layoutResourceId, parent,false);
holder = new ImageHolder();
holder.txtTitle = (TextView)row.findViewById(R.id.titleView);
holder.imgIcon=(ImageView)row.findViewById(R.id.iconView);
}
Hotels picture = data.get(position);
holder.txtTitle.setText(picture._name);
//convert byte to bitmap take from contact class
byte[] outImage=picture._image;
ByteArrayInputStream imageStream = new ByteArrayInputStream(outImage);
Bitmap theImage = BitmapFactory.decodeStream(imageStream);
holder.imgIcon.setImageBitmap(theImage);
return row;
}
static class ImageHolder
{
ImageView imgIcon;
TextView txtTitle;
}
}
最后是logcat的输出 12-12 19:43:20.665:E / MessageQueue-JNI(1183):java.lang.NullPointerException
12-12 19:43:20.665: E/MessageQueue-JNI(1183): at
athens.tourist.guide.HotelArrayAdapter.getView(HotelArrayAdapter.java:44)
12-12 19:43:20.665: E/MessageQueue-JNI(1183): at
athens.tourist.guide.HotelArrayAdapter.getView(HotelArrayAdapter.java:44)
12-12 19:43:20.665: E/MessageQueue-JNI(1183): at
android.widget.AbsListView.obtainView(AbsListView.java:2143)
12-12 19:43:20.665: E/MessageQueue-JNI(1183): at
android.widget.ListView.makeAndAddView(ListView.java:1831)
12-12 19:43:20.665: E/MessageQueue-JNI(1183): at
android.widget.ListView.fillUp(ListView.java:708)
答案 0 :(得分:3)
应用程序崩溃,因为convertView
只为null一次。尝试将holder对象存储为convertView的标记,然后检索它。 e.g
ImageHolder holder = null;
if(row==null){
LayoutInflater inflater =(LayoutInflater)
((Activity)context).getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(layoutResourceId, parent,false);
holder = new ImageHolder();
holder.txtTitle = (TextView)row.findViewById(R.id.titleView);
holder.imgIcon=(ImageView)row.findViewById(R.id.iconView);
row.setTag(holder);
}
holder = (ImageHolder) row.getTag();
答案 1 :(得分:0)
主要问题是当行不为空时,你永远不会初始化持有者所以,这是null并启动 NullPointerException 。
尝试更改此代码片段:
ImageHolder holder = null;
if(row==null){
LayoutInflater inflater =(LayoutInflater)
((Activity)context).getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(layoutResourceId, parent,false);
holder = new ImageHolder();
holder.txtTitle = (TextView)row.findViewById(R.id.titleView);
holder.imgIcon=(ImageView)row.findViewById(R.id.iconView);
}
对于这个:
ImageHolder holder = null;
if(row==null){
LayoutInflater inflater =(LayoutInflater)
((Activity)context).getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(layoutResourceId, parent,false);
holder = new ImageHolder();
holder.txtTitle = (TextView)row.findViewById(R.id.titleView);
holder.imgIcon=(ImageView)row.findViewById(R.id.iconView);
row.setTag(holder);
} else {
holder = (ImageHolder) row.getTag();
}