我正在尝试使用Picasso加载Facebook照片以获取列表视图。
我收到“ java.lang.IllegalArgumentException:Target不能为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)
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;
}
}
答案 0 :(得分:3)
据推测,holder.userpicture
是null
。 res/layout/list_item
没有名为userpicture
的窗口小部件,或者您需要清理项目(例如,项目&gt;从Eclipse主菜单中清除)。
另外,please use the three-parameter version of inflate()
,如果您将行布局更改为使用RelativeLayout
根,则不会遇到问题。
答案 1 :(得分:0)
它为我工作::)
在顶部声明您的图片视图:
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);
答案 2 :(得分:0)
它会很好用★★★
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;
}
}