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

原创 2015年11月21日 11:17:25

很奇怪的问题,先保存下来在,找时间来想想问什么!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;

    }



                    
版权声明:本文为博主原创文章,未经博主允许不得转载。

关于preparestatement行 报java.lang.NullPointerException

今天练习做jdbc利用工具类完成curd操作的这么个东西,在测试执行的时候一直报java.lang.NullPointerException 工具类是这样的 public class jdbc...

【Android】AndroidStudio空指针解决之:listview与adapter的使用报空 java.lang.NullPointerException

作者:程序员小冰 (转载请说明出处)博客地址:http://blog.csdn.net/qq_21376985长期维护的Android项目,里面包括常用功能实现,以及知识点详解, 当然还有Java...

web连接数据库时,报空指针java.lang.NullPointerException问题--可能的解决方式

序言: 好吧,第一次写CSDN博客,其实从一开始学计算机,到现在,快两年了,中间就不断的遇到问题,基本通过上这博客看的 确实挺好的  一直想什么时候开始  把自己遇到的问题和解决方法都写下来,毕竟每...

调用 RelativeLayout measure()报 java.lang.NullPointerException

典型重现环境 机型: Sony Ericsson Android version: 2.3.4 StackTrace: E/AndroidRuntime( 3579): FATAL EXCEP...
  • chthq
  • chthq
  • 2016年04月14日 12:52
  • 692

多数据源spring 报java.lang.NullPointerException

在调用test.java的时候就会报 Exception in thread "main" java.lang.NullPointerException at cn.m_solution....

Windows 平台运行spark-shell 报"java.lang.NullPointerException, not found: value sqlContext" error 解决办法

错误背景描述在spark 官网下载 spark的安装版:spark-1.6.3-bin-hadoop2.6.tgz,然后解压,配好环境变量,在window下CMD下运行spark-shell,启动sp...

这个servlet运行时老报java.lang.NullPointerException,请大家看看错在哪里。

import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.s...

java.lang.NullPointerException at org.hibernate.tuple.AbstractEntityTuplizer.createProxy(AbstractEn

这个问题很奇怪,如果 只查询单个对象的话,结果就没有问题,但是如果 进行复合查找的话,就会报如下错误。 在网上百度了下,找到了解决办法,就是将查询对象相关对象表映射文件设置lazy="fal...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:关于viewholder总是报Java.lang.NullPointerException。
举报原因:
原因补充:

(最多只允许输入30个字)