ArrayAdapter - ViewHolder 空指针错误
ArrayAdapter - ViewHolder null pointer error
我以为我掌握了如何使用 ViewHolders,但现在 运行 出现空指针异常。当我尝试为其中一个 itemTitleView 设置文本并且我不太明白我遗漏了什么时,就会发生这种情况。我有三种使用不同背景颜色的布局,并试图根据 ArrayList 的特定元素的 listViewItemType 使用每种布局。我有一个 Google,但对我来说还不是很清楚,我们将不胜感激。
PostItemAdapter.java
public class PostItemAdapter extends ArrayAdapter<Item>
{
private LayoutInflater inflater;
private ArrayList<Item> datas;
public static final int TYPE_short = 0;
public static final int TYPE_med = 1;
public static final int TYPE_long = 2;
public PostItemAdapter(Context context, int textViewResourceId, ArrayList<Item> objects)
{
super(context, textViewResourceId, objects);
inflater = ((Activity) context).getLayoutInflater();
datas = objects;
}
//class to hold view object references
static class ViewHolder
{
TextView itemTitleView;
TextView itemTitleView1;
TextView itemTitleView2;
ImageView itemThumbView;
}
@Override
public int getItemViewType(int position) {
return datas.get(position).viewtype;
}
public View getView(int position, View convertView, ViewGroup parent)
{
ViewHolder viewHolder;
int listViewItemType = getItemViewType(position);
if (convertView == null) {
convertView = inflater.inflate(R.layout.postitem, null);
viewHolder = new ViewHolder();
viewHolder.itemThumbView = (ImageView) convertView.findViewById(R.id.itemThumb);
if (listViewItemType == TYPE_short) {
viewHolder.itemTitleView = (TextView) convertView.findViewById(R.id.itemTitleLabel);
} else if (listViewItemType == TYPE_med) {
viewHolder.itemTitleView1 = (TextView) convertView.findViewById(R.id.itemTitleLabel1);
} else if (listViewItemType == TYPE_long) {
viewHolder.itemTitleView2 = (TextView) convertView.findViewById(R.id.itemTitleLabel2);
}
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
if (datas.get(position).itemThumbUrl == null)
{
viewHolder.itemThumbView.setImageResource(R.drawable.test);
}
viewHolder.itemTitleView.setText(datas.get(position).itemTitle);
viewHolder.itemTitleView1.setText(datas.get(position).itemTitle);
viewHolder.itemTitleView2.setText(datas.get(position).itemTitle);
return convertView;
}
}
将标题设置为TextView
时需要添加if else
语句。因为如果 listViewItemType
不等于 TYPE_short
,viewHolder.itemTitleView
将是 null
。
if (listViewItemType == TYPE_short) {
viewHolder.itemTitleView.setText(datas.get(position).itemTitle);
} else if (listViewItemType == TYPE_med) {
viewHolder.itemTitleView1.setText(datas.get(position).itemTitle);
} else if (listViewItemType == TYPE_long) {
viewHolder.itemTitleView2.setText(datas.get(position).itemTitle);
}
你的NullPointer exception
是因为你想设置itemTitleView
、itemTitleView1
和itemTitleView2
,而其中只有一个被初始化了。您必须将 if else 语句与初始化变量时以及将数据设置为 textViews
.
时相同
if (listViewItemType == TYPE_short) {
viewHolder.itemTitleView.setText(datas.get(position).itemTitle);
}
等等...
我以为我掌握了如何使用 ViewHolders,但现在 运行 出现空指针异常。当我尝试为其中一个 itemTitleView 设置文本并且我不太明白我遗漏了什么时,就会发生这种情况。我有三种使用不同背景颜色的布局,并试图根据 ArrayList 的特定元素的 listViewItemType 使用每种布局。我有一个 Google,但对我来说还不是很清楚,我们将不胜感激。
PostItemAdapter.java
public class PostItemAdapter extends ArrayAdapter<Item>
{
private LayoutInflater inflater;
private ArrayList<Item> datas;
public static final int TYPE_short = 0;
public static final int TYPE_med = 1;
public static final int TYPE_long = 2;
public PostItemAdapter(Context context, int textViewResourceId, ArrayList<Item> objects)
{
super(context, textViewResourceId, objects);
inflater = ((Activity) context).getLayoutInflater();
datas = objects;
}
//class to hold view object references
static class ViewHolder
{
TextView itemTitleView;
TextView itemTitleView1;
TextView itemTitleView2;
ImageView itemThumbView;
}
@Override
public int getItemViewType(int position) {
return datas.get(position).viewtype;
}
public View getView(int position, View convertView, ViewGroup parent)
{
ViewHolder viewHolder;
int listViewItemType = getItemViewType(position);
if (convertView == null) {
convertView = inflater.inflate(R.layout.postitem, null);
viewHolder = new ViewHolder();
viewHolder.itemThumbView = (ImageView) convertView.findViewById(R.id.itemThumb);
if (listViewItemType == TYPE_short) {
viewHolder.itemTitleView = (TextView) convertView.findViewById(R.id.itemTitleLabel);
} else if (listViewItemType == TYPE_med) {
viewHolder.itemTitleView1 = (TextView) convertView.findViewById(R.id.itemTitleLabel1);
} else if (listViewItemType == TYPE_long) {
viewHolder.itemTitleView2 = (TextView) convertView.findViewById(R.id.itemTitleLabel2);
}
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
if (datas.get(position).itemThumbUrl == null)
{
viewHolder.itemThumbView.setImageResource(R.drawable.test);
}
viewHolder.itemTitleView.setText(datas.get(position).itemTitle);
viewHolder.itemTitleView1.setText(datas.get(position).itemTitle);
viewHolder.itemTitleView2.setText(datas.get(position).itemTitle);
return convertView;
}
}
将标题设置为TextView
时需要添加if else
语句。因为如果 listViewItemType
不等于 TYPE_short
,viewHolder.itemTitleView
将是 null
。
if (listViewItemType == TYPE_short) {
viewHolder.itemTitleView.setText(datas.get(position).itemTitle);
} else if (listViewItemType == TYPE_med) {
viewHolder.itemTitleView1.setText(datas.get(position).itemTitle);
} else if (listViewItemType == TYPE_long) {
viewHolder.itemTitleView2.setText(datas.get(position).itemTitle);
}
你的NullPointer exception
是因为你想设置itemTitleView
、itemTitleView1
和itemTitleView2
,而其中只有一个被初始化了。您必须将 if else 语句与初始化变量时以及将数据设置为 textViews
.
if (listViewItemType == TYPE_short) {
viewHolder.itemTitleView.setText(datas.get(position).itemTitle);
}
等等...