关闭

关于viewholder总是报Java.lang.NullPointerException。

380人阅读 评论(0) 收藏 举报

很奇怪的问题,先保存下来在,找时间来想想问什么!viewholder总是报Java.lang.NullPointerException,如果在后面的if else选择加上判断holder!=null的话就会导致list view缓存问题,会出现重复出现之前的item的问题。

<span style="font-weight: normal;">public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.item_rank, null);
            </span><pre name="code" class="java" style="font-size: 14px; font-weight: bold;"><span style="font-weight: normal;"><span style="white-space:pre">	</span>holder.rankingNo = (TextView) convertView.findViewById(R.id.rankingNo);
        holder.photoLogo = (CircleImageView) convertView.findViewById(R.id.photoLogo);
        holder.effortNo = (TextView) convertView.findViewById(R.id.effortNo);
        holder.rankName = (TextView) convertView.findViewById(R.id.rankName);</span>
} else { holder = (ViewHolder) convertView.getTag(); } if (type.equals("popularity") && listPopularity != null &&holder!=null) { RankPopularityResult.Result.Member member = listPopularity.get(position); if (member.mbrType == 0) { ImageLoader.getInstance().displayImage(getImageUrl(member.photoId), holder.photoLogo, options); } else if (member.mbrType == 1) { ImageLoader.getInstance().displayImage(member.wcPhotoPath, holder.photoLogo, options); } NumberFormat currencyFormat = NumberFormat.getCurrencyInstance(); //int转化为钱的类型 holder.rankingNo.setText(String.valueOf(member.ranking)); holder.rankName.setText(member.nickName); holder.effortNo.setText(currencyFormat.format(member.effort)); Log.i("member.nickName", String.valueOf(member.nickName)); } else if (type.equals("company") && listCompany != null&& holder!=null) { Log.i("listCompany", String.valueOf(listCompany)); RankCompanyResult.Result.Member member = listCompany.get(position); ImageLoader.getInstance().displayImage(getImageUrl(member.sequareLogoPhoto), holder.photoLogo, options); // Log.i("", String.valueOf()); NumberFormat currencyFormat = NumberFormat.getCurrencyInstance(); holder.rankingNo.setText(String.valueOf(member.ranking)); holder.rankName.setText(member.companyName); Log.i("member.companyName", String.valueOf(member.companyName)); holder.effortNo.setText(currencyFormat.format(member.effort)); } // Log.i("", String.valueOf());//Java.lang.NullPointerException return convertView; } private class ViewHolder { private TextView rankingNo; private TextView rankName; private TextView effortNo; private CircleImageView photoLogo; }



这是参考google出来的修改后的代码!

<pre name="code" class="java">public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.item_rank, null);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        
        holder.rankingNo = (TextView) convertView.findViewById(R.id.rankingNo);
        holder.photoLogo = (CircleImageView) convertView.findViewById(R.id.photoLogo);
        holder.effortNo = (TextView) convertView.findViewById(R.id.effortNo);
        holder.rankName = (TextView) convertView.findViewById(R.id.rankName);
        if (type.equals("popularity") && listPopularity != null ) {

            RankPopularityResult.Result.Member member = listPopularity.get(position);

            if (member.mbrType == 0) {
                ImageLoader.getInstance().displayImage(getImageUrl(member.photoId), holder.photoLogo, options);
            } else if (member.mbrType == 1) {
                ImageLoader.getInstance().displayImage(member.wcPhotoPath, holder.photoLogo, options);
            }
            NumberFormat currencyFormat = NumberFormat.getCurrencyInstance(); 
            holder.rankingNo.setText(String.valueOf(member.ranking));
            holder.rankName.setText(member.nickName);
            holder.effortNo.setText(currencyFormat.format(member.effort));
            Log.i("member.nickName", String.valueOf(member.nickName));

        } else if (type.equals("company") && listCompany != null ) {
            Log.i("listCompany", String.valueOf(listCompany));
            RankCompanyResult.Result.Member member = listCompany.get(position);
            ImageLoader.getInstance().displayImage(getImageUrl(member.sequareLogoPhoto), holder.photoLogo, options);
            // Log.i("", String.valueOf());
            NumberFormat currencyFormat = NumberFormat.getCurrencyInstance();
            holder.rankingNo.setText(String.valueOf(member.ranking));
            holder.rankName.setText(member.companyName);
            Log.i("member.companyName", String.valueOf(member.companyName));
            holder.effortNo.setText(currencyFormat.format(member.effort));
        }
       // Log.i("", String.valueOf());
//Java.lang.NullPointerException
        return convertView;
    }

    private class ViewHolder {
        private TextView rankingNo;
        private TextView rankName;
        private TextView effortNo;
        private CircleImageView photoLogo;

    }



   
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场