我有一個BaseFragment
所謂LeaderboardFragment延伸Fragment
:Java數組中的繼承:爲什麼這是一個類型不匹配?
public class LeaderboardFragment extends Fragment
當我確定在FragmentActivity
下一個,我得到類型不匹配:
Fragment[] fragments = new LeaderboardFragment[] {new Fragment1(),
new Fragment2(), new Fragment3()};
而且這些片段都可以擴展LeaderboardFragment
。像這樣的東西應該是可能的,因爲它們只是繼承那個超級類。
編輯 這是我FragmentActivity看起來像:
public class LeaderboardTabHost extends FragmentActivity {
ViewPager myViewPager;
Fragment[] fragments = new Fragment[] {new Fragment1(), new Fragment2(), new Fragment3()};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_collection_object);
MyPagerAdapter adapter = new MyPagerAdapter(getSupportFragmentManager());
myViewPager = (ViewPager) findViewById(R.id.pager);
myViewPager.setAdapter(adapter);
myViewPager.setCurrentItem(1);
}
private class MyPagerAdapter extends FragmentPagerAdapter {
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int index) {
return fragments[index];
}
public int getCount() {
return 3;
}
@Override
public void destroyItem(View arg0, int arg1, Object arg2) {
((ViewPager) arg0).removeView((View) arg2);
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
// TODO Auto-generated method stub
return arg0 == ((View) arg1);
}
@Override
public Parcelable saveState() {
return null;
}
}
public class Fragment1 extends LeaderboardFragment {
public Fragment1() {
super();
setTag("time");
}
}
public class Fragment2 extends LeaderboardFragment {
public Fragment2() {
super();
setTag("distance");
}
}
public class Fragment3 extends LeaderboardFragment {
public Fragment3() {
super();
setTag("speed");
}
}
}
以及確切的錯誤是:
//Type mismatch: Cannot convert from LeaderboardTabhost.Fragment1 to Fragment
顯示'FragmentX' * *和*顯示* exact *錯誤消息(多行縮進和全部)的定義。 – 2012-06-23 16:59:24