4

我正在处理片段并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;

尝试创建页面适配器时出现错误的主要活动

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

}
4

1 回答 1

1

您不能创建抽象类的实例。

于 2015-07-18T17:40:18.013 回答