Android:活动中的Nullpointer异常错误

时间:2011-05-06 13:11:40

标签: android nullpointerexception

我在ListViewAdapter中有这个getView方法:

public static class ViewHolder{
        public TextView textTitle;
        public ImageView image;
    }

  public View getView(int position, View convertView, ViewGroup parent)
    {
        Project pro = getItem(position);

        View vi=convertView;
        ViewHolder holder;
        if(convertView==null){
            vi = inflater.inflate(R.layout.listitems, null);
            holder=new ViewHolder();
            holder.textTitle=(TextView)vi.findViewById(R.id.txt_title);;
            holder.image=(ImageView)vi.findViewById(R.id.image);
            vi.setTag(holder);
        }
        else
        holder=(ViewHolder)vi.getTag();
        holder.textTitle.setText(pro.project_title);
        holder.image.setTag(pro);
        imageLoader.DisplayImage(pro.smallImageUrl, activity, holder.image);
        return vi;

    }

因为这是列表视图,它显示图像和文本。 另一方面,我有一个活动,我想在其中应用imageLoader.DisplayImage方法只显示图像。

基于ListView适配器,我在一个活动中创建了这个:

imageLazy(image1, Main.this, prjcts.get(randomIndex1));

public void imageLazy(final ImageView image, Activity activity, Project pro)
    {
    imageLoaderx.DisplayImage(pro.smallImageUrl, activity, image);
    }

然后我的应用程序崩溃了。 Logcat报告Nullpointer异常错误,并使用imageLazy方法报告错误。

有人可以帮我解决问题吗?这样我的方法可以显示图像而不会出错? 非常感谢你

1 个答案:

答案 0 :(得分:1)

imageLoaderX尚未初始化且为null。您可以通过创建新对象或在其他位置获取非空引用来解决此问题。