尝试在空对象引用上调用虚方法'android.view.View android.view.View.findViewById(int)'

时间:2016-10-29 22:06:44

标签: android listview android-fragments nullpointerexception

我在logcat中有错误

java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference

尝试查看列表中的图片为此代码创建一个类 我认为方法中的错误查看getView但我不知道它究竟在哪里我认为可能在这里的thumbNail定义

public class useradaptor extends BaseAdapter {


             private Activity activity;
             private LayoutInflater inflater;
             private List<user> userItems;

ImageLoader imageLoader = AppController.getInstance().getImageLoader();

public useradaptor(Activity activity, List<user> userItems) {
    this.activity = activity;
    this.userItems = userItems;
}

@Override
public int getCount() {
    return userItems.size();
}

@Override
public Object getItem(int location) {
    return userItems.get(location);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    user u = userItems.get(position);
    if (imageLoader == null)
        imageLoader = AppController.getInstance().getImageLoader();
    NetworkImageView thumbNail = (NetworkImageView) convertView.findViewById(R.id.imageview);
    thumbNail.setImageUrl(u.getPicture(), imageLoader);

    Cache cache = AppController.getInstance().getRequestQueue().getCache();
    Cache.Entry entry = cache.get(u.getPicture());
    if(entry != null){
        try {
            String data = new String(entry.data, "UTF-8");
            // handle data, like converting it to xml, json, bitmap etc.,
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }else{
        // cached response doesn't exists. Make a network call here
    }
    return convertView;
  }
}

1 个答案:

答案 0 :(得分:1)

如果convertView没有为您提供回收的行,则

null将为ListView。当convertViewnull时,您需要通过某种方式创建列表行。通常情况下,您使用LayoutInflater,最好是通过getLayoutInflater()上的Activity调用{。}}。