无法实例化类型PagerAdapter

时间:2014-12-10 07:48:31

标签: android android-fragments android-pageradapter fragmentstatepageradapter pageviews

我正在使用片段和pageview尝试使用水平寻呼机,我收到以下错误:

Cannot instantiate the type PagerAdapter.

我知道PagerAdapter是一个抽象类,所以我创建了一个继承自PagerAdapter并使用它的新类。 但没有解决它。

这是我的代码:

主要活动导入

import java.util.List;
import java.util.Vector;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
//import com.example.prova.PagerAdapter;
//import android.support.v4.view.PagerAdapter;

当我尝试创建pageradapter时出现错误的主要活动

public class MainActivity extends ActionBarActivity {

    List<Fragment> fragments = new Vector<Fragment>();
    private ViewPager mPager;
    PagerAdapter mPagerAdapter; 
    //mPager.setCurrentItem(2);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

     // creating fragments and adding to list
        fragments.add(Fragment.instantiate(this,Timeline.class.getName()));
        fragments.add(Fragment.instantiate(this,Postit.class.getName()));
        fragments.add(Fragment.instantiate(this,Frase.class.getName()));
        fragments.add(Fragment.instantiate(this,Impostazioni.class.getName()));

     // creating adapter and linking to view pager
        this.mPagerAdapter=new PagerAdapter(super.getSupportFragmentManager(),fragments); //I GOT HERE THE ERROR
        mPager = (ViewPager) super.findViewById(R.id.pager);
        mPager.setAdapter(this.mPagerAdapter);
    }

这是我的PagerAdapter

public abstract class PagerAdapter extends FragmentPagerAdapter {

   // fragments to instantiate in the viewpager
   private List<Fragment> fragments;

   // constructor
   public PagerAdapter(FragmentManager fm,List<Fragment> fragments) {
      super(fm);
      this.fragments = fragments;
   }

   // return access to fragment from position, required override
   @Override
   public Fragment getItem(int position) {
      return this.fragments.get(position);
   }

   // number of fragments in list, required override
   @Override
   public int getCount() {
      return this.fragments.size();
   }

}

1 个答案:

答案 0 :(得分:1)

您无法创建抽象类的实例。