0

getViewの1サイクル後に、nullポインタがスローされます。つまり、リストの最初の要素は正常に返されますが、2番目の要素ではクラッシュします。

ホルダーがnullであると表示されます。ビットを参照し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;
    }
4

1 に答える 1

2

あなたが忘れてしまった

row.setTag(holder);

行を膨らませてホルダー値を設定した後。

このように、row.tagを再利用すると、nullに設定されます。

于 2012-12-03T19:41:26.860 に答える