Josh Beckwith
Josh Beckwith

Reputation: 1538

Android programming; getting a small error

I'm working on an Android Application and I'm using the .setAdapter command and I'm getting the red squiggle under it. Eclipse tells me this. "The method setAdapter(PagerAdapter) in the type ViewPager is not applicable for the arguments (Upperbody.MyPagerAdapter)"

Specifically right here;

myPager.setAdapter(adapter); 

Here's my code;

package com.example.gymbuddy; 

import android.app.Activity; 
import android.content.Context; 
import android.os.Bundle; 
import android.os.Parcelable; 
import android.support.v4.view.ViewPager; 
import android.view.LayoutInflater; 
import android.view.View; 

public class Upperbody extends Activity { 

   @Override 
   public void onCreate(Bundle savedInstanceState) { 
   super.onCreate(savedInstanceState); 
   setContentView(R.layout.activity_upperbody); 

   MyPagerAdapter adapter = new MyPagerAdapter(); 
   ViewPager myPager = (ViewPager) findViewById(R.id.myfivepanelpager); 
   myPager.setAdapter(adapter); 
   myPager.setCurrentItem(2); } 

   public class MyPagerAdapter extends Activity { 

    public int getCount() { 
        return 9; 
    } 

    public Object instantiateItem(View collection, int position) { 

        LayoutInflater inflater = (LayoutInflater) collection.getContext() 
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

        int resId = 0; 
        switch (position) { 
        case 0: 
            resId = R.layout.upper0; 
            break; 
        case 1: 
            resId = R.layout.upper1; 
            break; 
        case 2: 
            resId = R.layout.upper2; 
            break; 
        case 3: 
            resId = R.layout.upper3; 
            break; 
        case 4: 
            resId = R.layout.upper4; 
            break; 
        case 5: 
           resId = R.layout.upper5; 
           break; 
        case 6: 
           resId = R.layout.upper6; 
           break; 
        case 7: 
           resId = R.layout.upper7; 
           break; 
        case 8: 
           resId = R.layout.upper8; 
           break; } 

        View view = inflater.inflate(resId, null); 

        ((ViewPager) collection).addView(view, 0); 

        return view; } 

    public void destroyItem(View arg0, int arg1, Object arg2) { 
        ((ViewPager) arg0).removeView((View) arg2); } 

    public boolean isViewFromObject(View arg0, Object arg1) { 
        return arg0 == ((View) arg1); } 

    public Parcelable saveState() { 
        return null; } 

   } 

} 

Upvotes: 1

Views: 524

Answers (3)

Bhagya Gaddam
Bhagya Gaddam

Reputation: 11

MyPagerAdapter should implement PagerAdapter not an Activity.

Please check this link.

Upvotes: 1

Hang Guan
Hang Guan

Reputation: 102

Your MyPagerAdapter needs to be a child class of PagerAdapter, not activity.

Change

public class MyPagerAdapter extends Activity { 

to

public class MyPagerAdapter extends PagerAdapter { 

Upvotes: 0

nachoburton
nachoburton

Reputation: 71

I think your problem is simply that you have not made MyPageAdapter extend PageAdapter. Instead, it is extending Activity.

Upvotes: 1

Related Questions