【问题标题】:Picasso IllegalArgumentExceptionPicasso IllegalArgumentException
【发布时间】:2014-06-01 23:55:22
【问题描述】:

我正在尝试使用 Picasso 为列表视图加载 Facebook 照片。

我收到“java.lang.IllegalArgumentException: Target must not be null

在这一行:

.into(holder.userpicture);

如果用户图片 ImageView 是“目标”,那么我不明白。我的猜测是它与我的适配器有关,但我不知道是什么。

非常感谢所有帮助!

编辑

我发现了问题。我犯了一个粗心的错误。在这一行,我忘记了 convertView 部分:

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)

我的适配器:

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;
    }
}

【问题讨论】:

    标签: android picasso


    【解决方案1】:

    大概holder.userpicturenullres/layout/list_item 没有名为 userpicture 的小部件,或者您需要清理您的项目(例如,从 Eclipse 主菜单中选择 Project > Clean)。

    另外,please use the three-parameter version of inflate(),因此如果您将行布局更改为使用 RelativeLayout 根目录,您不会遇到问题。

    【讨论】:

      【解决方案2】:

      它对我有用 :)

      第一步:

      像这样在顶部声明您的图像视图:

      public class BaseAdapterShow extends BaseAdapter {
          ImageView imgView;
      }
      

      第二步:

      在您的 getView() 中参考图像

      示例:

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

      第三步:

      像这样直接使用到 Picasa:

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

      【讨论】:

        【解决方案3】:

        它会很好用★★★

        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;
        }
        

        }

        【讨论】:

          猜你喜欢
          • 2015-01-03
          • 2019-09-10
          • 1970-01-01
          • 2023-03-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多