-1

I got this error while trying to use Picasso with my adapter. I use Picasso because I get out-of-memory error on phones while it's working fine on tablets.

Am I using Picasso the wrong way? Cause I don't see any errors.

UPDATE THIS IS WHAT I EDIT

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;

I'm getting target must not be null Here's the code:

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

My 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>
user3334111
  • 21
  • 1
  • 7

1 Answers1

0

From Picasso's official website, one of the correct syntax is:

Picasso.with(context).load(R.drawable.landing_screen).into(imageView1);

Assume flag[] contains resource ID for the images, the problem with your code,

Picasso.with(context).load(flag[position]).fit().into((Target) itemView);

is that itemView is RelativeLayout, not an ImageView.

Try this (changes are indicated by comment)

@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;
}
Andrew T.
  • 4,701
  • 8
  • 43
  • 62
  • Got a problem a minute ago. I think its picasso. Its not loading any images just giving me black screen but no errors. why picasso don't load images – user3334111 Sep 08 '14 at 06:28
  • Does it always happen or not? Unfortunately, I never use Picasso so I'm not sure about that one. If it always happen, you could try to find similar questions on Google or here, and if you really must, ask a new question while linking to this question. – Andrew T. Sep 08 '14 at 06:31
  • Well it always happen – user3334111 Sep 08 '14 at 06:35
  • Honestly, I'm not sure about that. Probably my code isn't the correct way to load images using Picasso as I never used it before. It might be caused by the `ViewPager` too, but I'm also not sure about it. – Andrew T. Sep 08 '14 at 06:38
  • 1
    Anyway Thanks for the help! I got it working i added android:orientation="vertical" on my layout. I'm trying something because I want to make phones to allow both orientation but landscape only for tablets. Thank you very much! – user3334111 Sep 08 '14 at 06:56