Picasso IllegalArgumentException

2019-08-09 10:33发布

问题:

I'm trying to use Picasso to load facebook photos for a listview.

I'm getting "java.lang.IllegalArgumentException: Target must not be null"

on this line:

.into(holder.userpicture);

If the userpicture ImageView is the 'target', then I don't understand. My guess is that it has something to do with my adapter, but I can't figure out what.

All help greatly appreciated!

EDIT

I found the problem. I made a careless mistake. On this line, I forgot the convertView part:

holder.userpicture=(ImageView)findViewById(R.id.userpicture);

Logcat:

06-01 16:37:14.392: E/AndroidRuntime(7885): java.lang.IllegalArgumentException: Target must not be null.
06-01 16:37:14.392: E/AndroidRuntime(7885):     at com.squareup.picasso.RequestCreator.into(RequestCreator.java:479)
06-01 16:37:14.392: E/AndroidRuntime(7885):     at com.squareup.picasso.RequestCreator.into(RequestCreator.java:462)
06-01 16:37:14.392: E/AndroidRuntime(7885):     at com.example.mywebsite.AllProductsActivity$MyAdapter.getView(AllProductsActivity.java:327)

MyAdapter:

public class MyAdapter extends ArrayAdapter<HashMap<String, String>> {

    Context context;
    int resourceId;
    LayoutInflater inflater;
    private Context mContext;


    ArrayList<HashMap<String, String>>  items;
    public MyAdapter (Context context, int resourceId, ArrayList<HashMap<String, String>> items)
    {
        super(context, resourceId, items);
        mContext = context;
        this.items =items;
        inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent){
        final ViewHolder holder;
        if (convertView == null){

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

            holder = new ViewHolder();
            holder.name = (TextView)convertView.findViewById(R.id.name);
            holder.userpicture=(ImageView)findViewById(R.id.userpicture);
            convertView.setTag(holder);

        } else {

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

        HashMap<String,String> item = (HashMap<String,String> ) items.get(position);
        if (item != null)
        {

            String facebookProfilePicUrl = "http://graph.facebook.com/"+TAG_FACEBOOKID+"/picture?width=100&height=100";

           Picasso.with(mContext).load(facebookProfilePicUrl)
.into(holder.userpicture);

            holder.name.setText(item.get(TAG_FACEBOOKID));


        }

        return convertView;
    }

    public class ViewHolder
    {
        TextView    name;
        ImageView userpicture;
    }
}

回答1:

Presumably, holder.userpicture is null. Either res/layout/list_item does not have a widget named userpicture, or you need to clean your project (e.g., Project > Clean from the Eclipse main menu).

Also, please use the three-parameter version of inflate(), so you do not run into problems if you change your row layout to use a RelativeLayout root.



回答2:

Its working for me : :)

Step1 :

Declare your image view on the top like this :

public class BaseAdapterShow extends BaseAdapter {
    ImageView imgView;
}

Step2 :

In your getView() take reference of image

Example:

imgView = (ImageView) convertView.findViewById(R.id.catrImg);

Step3:

Use Directly to the Picasa like this:

Picasso.with(context).load(imageurl)
    .placeholder(R.drawable.profilepic)
    .error(R.drawable.profilepic)
    .resize(250, 200).into(imgView);


回答3:

It will work fine ★★★

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

    if (convertView == null){

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

        ViewHolder holder = new ViewHolder();
        holder.name = (TextView)convertView.findViewById(R.id.name);
        holder.userpicture=(ImageView)findViewById(R.id.userpicture);
        convertView.setTag(holder);

    } else {

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

    ViewHolder holder = (ViewHolder) convertView.getTag();

    HashMap<String,String> item = (HashMap<String,String> ) items.get(position);
    if (item != null)
    {

        String facebookProfilePicUrl = "http://graph.facebook.com/"+TAG_FACEBOOKID+"/picture?width=100&height=100";

       Picasso.with(mContext).load(facebookProfilePicUrl).into(holder.userpicture);

        holder.name.setText(item.get(TAG_FACEBOOKID));


    }

    return convertView;
}

public class ViewHolder
{
    TextView    name;
    ImageView userpicture;
}

}