在ListView适配器上设置图像资源

时间:2018-03-06 08:55:31

标签: java android listview android-adapter

我有一个这样的适配器

if(modelSalesAct.getStatus().equals("2"))
    {
        viewHolder.ivStatus.setImageResource(R.drawable.ic_black);
    }
    else
    if(modelSalesAct.getStatus().equals("3")||modelSalesAct.getStatus().equals("4"))
    {
        viewHolder.ivStatus.setImageResource(R.drawable.ic_yellow);
    }
    else
    if(modelSalesAct.getStatus().equals("5"))
    {
        viewHolder.ivStatus.setImageResource(R.drawable.ic_red);
    }

这是为了设置与条件状态不同的图像匹配。但是我收到一条错误消息

  

java.lang.NullPointerException:尝试调用虚方法' void android.widget.ImageView.setImageResource(int)'在null对象引用上                         在com.kreators.nvmobile.dashboard.AdapterSalesAct.getView(AdapterSalesAct.java:133)

错误在线停止我是来自我的drawable的setImageResource。为什么会这样?我不知道会发生什么。任何人都可以帮助我吗?

仅供参考,这是我的ViewAdapter

        ViewHolder viewHolder; // view lookup cache stored in tag

    final View result;

    if (convertView == null) {

        viewHolder = new ViewHolder();
        LayoutInflater inflater = LayoutInflater.from(getContext());
        convertView = inflater.inflate(R.layout.row_sales_act, parent,
                false);
        viewHolder.ivHeader = (ImageView)
                convertView.findViewById(R.id.ivHeader);
        viewHolder.txtCUSTID = (TextView)
                convertView.findViewById(R.id.ADAPTERCUSTID);
        viewHolder.txtSalesName = (TextView)
                convertView.findViewById(R.id.ADAPTERSALESNAME);
        viewHolder.txtSales = (TextView)
                convertView.findViewById(R.id.ADAPTERSALES);
        viewHolder.txtChannel = (TextView)
                convertView.findViewById(R.id.tvCustomer);
        viewHolder.ivStatus = (ImageView)
                convertView.findViewById(R.id.ivStatus);
        viewHolder.txtTarget = (TextView)
                convertView.findViewById(R.id.tvTarget);
        viewHolder.txtSI = (TextView)
                convertView.findViewById(R.id.tvSI);
        viewHolder.txtPercentPSI = (TextView)
                convertView.findViewById(R.id.tvPercentPSI);
        viewHolder.txtPercentSI = (TextView)
                convertView.findViewById(R.id.tvPercentSI);
        viewHolder.txtSO = (TextView)
                convertView.findViewById(R.id.tvSO);
        viewHolder.txtAR = (TextView)
                convertView.findViewById(R.id.tvAR);

        result=convertView;
        convertView.setTag(viewHolder);
    } else {
        viewHolder = (ViewHolder) convertView.getTag();
        result=convertView;
    }

    viewHolder.txtCUSTID.setText(modelSalesAct.getCUSTID());
    viewHolder.txtSalesName.setText(modelSalesAct.getSALESNAME());
    viewHolder.txtSales.setText(modelSalesAct.getSALES());
    viewHolder.txtChannel.setText(modelSalesAct.getChannel());

    if(modelSalesAct.getStatus().equals("2"))
    {
        viewHolder.ivStatus.setImageResource(R.drawable.ic_black);
    }
    else
    if(modelSalesAct.getStatus().equals("3")||modelSalesAct.getStatus().equals("4"))
    {
        viewHolder.ivStatus.setImageResource(R.drawable.ic_yellow);
    }
    else
    if(modelSalesAct.getStatus().equals("5"))
    {
        viewHolder.ivStatus.setImageResource(R.drawable.ic_red);
    }
    viewHolder.txtTarget.setText(modelSalesAct.getTarget());
    viewHolder.txtSI.setText(modelSalesAct.getSI());
    viewHolder.txtPercentSI.setText(modelSalesAct.getPercentSI());
    viewHolder.txtPercentPSI.setText(modelSalesAct.getPercentPSI());
    viewHolder.txtSO.setText(modelSalesAct.getSO());
    viewHolder.txtAR.setText(modelSalesAct.getAR());

    return convertView;

3 个答案:

答案 0 :(得分:2)

h_att_list

ViewAdapter

ImageView ivStatus = (ImageView)convertView.findViewById(R.id.ivStatus);

答案 1 :(得分:0)

检查ivStatusrow_sales_act.xml的{​​{1}}是否为Imageview,问题是当您尝试查找在您正在查看的地方找不到ID的视图时,它将返回null

答案 2 :(得分:0)

您无法在此行中找到ivHeader的ID。

viewHolder.ivHeader = (ImageView)
                convertView.findViewById(R.id.ivHeader);

因此你得到空指针异常 放一个调试器&确保ivHeader布局文件

中是否存在row_sales_act.xml