Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Universal image loader not work

I'm a beginning programmer. I'm trying use Universal image loader to display images parsed from site, but it is not work. What am I doing wrong?

Thanks.

This is a test programm.

package com.example.imageloader;

public class MainActivity extends ListActivity {

private final String URL = "Site_URL";
private ArrayList<Order> listItems = null;
private myAdapter myAdapter;
private ProgressDialog pd;  
ImageLoader imageLoader;    
DisplayImageOptions options;    
String imgSrc;
ListView lv;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    listItems = new ArrayList<Order>(); 
    this.myAdapter = new myAdapter(this, R.layout.list_item, listItems);
    setListAdapter(this.myAdapter);

    new Parse().execute(URL);

    lv = getListView();

    imageLoader = ImageLoader.getInstance();
    imageLoader.init(ImageLoaderConfiguration.createDefault(MainActivity.this));

    options = new DisplayImageOptions.Builder()
    .showStubImage(R.drawable.ic_launcher)
    .showImageForEmptyUri(R.drawable.ic_launcher)
    .cacheOnDisc()
    .cacheInMemory()
    .build();
}


private class Parse extends AsyncTask<String, Integer, ArrayList<Order>> {
    @Override
    protected void onPreExecute() {
        pd = new ProgressDialog(MainActivity.this);
        pd.setMessage("Loading");
        pd.show();
    }

    @Override
    protected ArrayList<Order> doInBackground(String... urls) {
        listItems.clear();              
        try {
            Document doc = Jsoup.connect(URL).get();
            Elements img = doc.select("img");
            for (Element src : img) {
                String imgSrc = src.attr("src");

                System.out.println(imgSrc);                         

                Order o = new Order();
                o.setLink(imgSrc);
                o.setTextName(imgSrc);
                listItems.add(o);                   
            }               

        } catch (IOException e) {
            e.printStackTrace();
        }
        return listItems;
    }

    @Override
    protected void onPostExecute(ArrayList<Order> listItems) {  
        myAdapter = new myAdapter(MainActivity.this, R.layout.list_item, listItems);
        setListAdapter(myAdapter);
        myAdapter.notifyDataSetChanged();
        pd.dismiss();
    }
}
}

Custom adater I get an error here

package com.example.imageloader;

public class myAdapter extends ArrayAdapter<Order> {

private LayoutInflater lInflater;
private ArrayList<Order> data;
DisplayImageOptions options;
ImageLoader imageLoader;
private Order o;
String imgSrc;  

public myAdapter(Context c, int listItem, ArrayList<Order> data) {
    super(c, listItem, data);
    this.data = data;
    lInflater = LayoutInflater.from(c);
}

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

    ViewHolder holder = null;

    if (convertView == null) {
        convertView = lInflater.inflate(R.layout.list_item, null);
        holder = new ViewHolder();
        holder.img = (ImageView) convertView.findViewById(R.id.image);
        holder.textName = (TextView) convertView.findViewById(R.id.text);
        convertView.setTag(holder);

        holder.textName.setOnClickListener(new OnItemClickListener(position));

    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    o = data.get(position);
    holder.textName.setText(o.getTextName());
    holder.img.setTag(o);

        try {
            imageLoader.displayImage(o.getLink(), holder.img, options); // NullPointerException here!
        } catch (NullPointerException e) {
            e.printStackTrace();
        }
    } else {            
        return null;            
    }
    return convertView;


static class ViewHolder {
    ImageView img;
    TextView textName;
}
}

Log

01-16 11:55:04.480: W/System.err(19105): java.lang.NullPointerException
01-16 11:55:04.496: W/System.err(19105):    at          com.example.imageloader.myAdapter.getView(myAdapter.java:62)
like image 207
Azat-777 Avatar asked Dec 09 '22 18:12

Azat-777


2 Answers

you should add this code

imageLoader = ImageLoader.getInstance();
    imageLoader.init(ImageLoaderConfiguration.createDefault(c));

in your constructor of myAdapter. and also checks the id of imageview, is it correct or at right place where you are referencing it to

holder.img = (ImageView) convertView.findViewById(R.id.image);
like image 175
skygeek Avatar answered Dec 11 '22 08:12

skygeek


Just add

imageLoader = ImageLoader.getInstance();

to myAdapter constructor.

like image 23
nostra13 Avatar answered Dec 11 '22 07:12

nostra13