我正在使用Android应用程序,并且正在使用.setAdapter命令,并且在其下出现红色花键。 Eclipse告诉了我这一点。 “ ViewPager类型的方法setAdapter(PagerAdapter)不适用于参数(Upperbody.MyPagerAdapter)”

特别是在这里;

myPager.setAdapter(adapter);


这是我的代码;

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

   }

}

最佳答案

我认为您的问题很简单,您没有让PagerAdapter扩展PagerAdapter。相反,它扩展了Activity。

关于java - Android编程;出现一个小错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12398622/

10-08 21:22