我尝试将Picasso与我的适配器一起使用时出现此错误。我使用Picasso是因为我在手机上出现了内存错误,而它在平板电脑上运行正常。
我以错误的方式使用毕加索吗?因为我没有看到任何错误。
更新这就是我编辑的内容
ImageView imgButtons;
imgButtons = (ImageView) findViewById(R.id.items);
Picasso.with(context).load(flag[position]).fit().into(imgButtons);
// Add viewpager_item.xml to ViewPager
((ViewPager) container).addView(imgButtons);
return imgButtons;
我获得目标不能为空 这是代码:
public class ViewPagerAdapter extends PagerAdapter {
// Declare Variables
Context context;
int[] flag;
String[] rank;
LayoutInflater inflater;
public ViewPagerAdapter(Context context, int[] flag, String[] rank) {
this.context = context;
this.flag = flag;
this.rank = rank;
}
@Override
public int getCount() {
return rank.length;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((RelativeLayout) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView imgflag;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.app3x, container, false);
// Locate the ImageView in viewpager_item.xml
// imgflag = (ImageView) itemView.findViewById(R.id.items);
// Capture position and set to the ImageView
// imgflag.setImageResource(flag[position]);
Picasso.with(context).load(flag[position]).fit().into((Target) itemView);
// Add viewpager_item.xml to ViewPager
((ViewPager) container).addView(itemView);
return itemView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
// Remove viewpager_item.xml from ViewPager
((ViewPager) container).removeView((RelativeLayout) object);
}
}
我的XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical"
android:background="@color/White">
<ImageView
android:id="@+id/items"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentRight="true"
android:background="@color/White"
android:scaleType="fitXY" />
</RelativeLayout>
答案 0 :(得分:0)
来自Picasso的官方网站,其中一种正确的语法是:
Picasso.with(context).load(R.drawable.landing_screen).into(imageView1);
假设flag[]
包含图像的资源ID,代码存在问题,
Picasso.with(context).load(flag[position]).fit().into((Target) itemView);
itemView
是RelativeLayout
,而不是ImageView
。
尝试此操作(更改以评论表示)
@Override
public Object instantiateItem(ViewGroup container, int position) {
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.app3x, container, false);
ImageView imgflag = (ImageView) itemView.findViewById(R.id.items); // need this ImageView
Picasso.with(context).load(flag[position]).fit().into(imgflag); // use ImageView
((ViewPager) container).addView(itemView);
return itemView;
}