我的自定义列表视图适配器抛出空指针异常我无法弄清楚为什么

时间:2012-12-03 19:38:03

标签: android

在getView的一个周期后抛出空指针。即列表中的第一个元素返回正常但它在第二个元素上崩溃。

它表示持有人为空。引用holder.textName.setText(station)位。

知道为什么吗?

...
static class StationHolder {
        TextView textName;
        ImageView imgStationImage;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        StationHolder holder = null;

        Typeface tfBoldCon = Typeface.createFromAsset(context.getAssets(),
                "Roboto-BoldCondensed.ttf");
        Typeface tfCon = Typeface.createFromAsset(context.getAssets(),
                "Roboto-Condensed.ttf");
        Typeface tfLight = Typeface.createFromAsset(context.getAssets(),
                "Roboto-Light.ttf");

        if (row == null) {
            LayoutInflater inflater = ((Activity) context).getLayoutInflater();
            row = inflater.inflate(viewResourceId, parent, false);

            holder = new StationHolder();
            holder.textName = (TextView) row.findViewById(R.id.station_name);
            holder.textName.setTypeface(tfCon);
            holder.imgStationImage = (ImageView) row
                    .findViewById(R.id.station_image);

        } else {
            holder = (StationHolder) row.getTag();
        }

        final String station = stations.get(position);
        Log.i("", station);

        if (station != null) {
            holder.textName.setText(station);
        }
        return row;
    }

1 个答案:

答案 0 :(得分:2)

你忘记了

row.setTag(holder);

在给行充气并设置持有人值之后。

这样,当你重用row.tag时,它被设置为null。

相关问题