开发者

Java null pointer exception when i pass this three parameter to the lazy loading class

开发者 https://www.devze.com 2023-03-19 11:10 出处:网络
@Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder; if(convertView == null)
@Override
    public View getView(int position, View convertView, ViewGroup parent)   
    {
        ViewHolder holder;
        if(convertView == null)
        { 
            convertView = inflater.inflate(R.layout.photodata,null);

            myHolder = new ViewHolder();
            try
            {  
                myHolder.imgPhoto = (ImageView) convertView.findViewById(R.id.imgPhoto);            
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
            convertView.setTag(myHolder);
        }
        else
        {
            myHolder = (ViewHolder)convertView.getTag();
        }
        System.out.println("String image is" + getItem(position).photo);
        myHolder.imgPhoto.setTag(getItem(position).photo);
        System.out.println("String image is" + getItem(p开发者_如何学编程osition).photo);
        imageLoader.DisplayImage(getItem(position).photo,activity, myHolder.imgPhoto);

        return convertView;
    }

Error:

FATAL EXCEPTION: main
java.lang.NullPointerException
 at.com.android.adapter.PhotoAdater.getView(PhotoAdater.java:59)
 at.android.widget.AbsListView.obtainView(AbsListView.java:1315)


This code you are declaring ,

 ViewHolder holder;

but you are using different object,

myHolder.imgPhoto= (ImageView) convertView.findViewById(R.id.imgPhoto);   

change as,

 holder.imgPhoto= (ImageView) convertView.findViewById(R.id.imgPhoto); 

and also change Layoutinflater like this,

convertView = inflater.inflate(R.layout.photodata,null,false);

this code reduced error


There seems to be a problem in the code you have pasted here. Seeing the code I assume

  ViewHolder holder;

should actually be

  ViewHolder myHolder;

In that case, you have created the instance using new in the if condition, but in the else, it is not done. So ,myHolder points to null. Think this is the reason for the NullPointerException


My guess without line numbers and/or knowing what you're passing to this method...if you attempt to call this method with position == null then it will throw an NPE because you're passing null to a primitive.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号