Reputation: 389
I am new to Android and following some tutorials online. However, fragments appeared to have changed since the tutorial was published. Now I am stuck on an error that I cannot seem to resolve. I am getting the error "The return type is incompatible with FragmentPagerAdapter.getItem(int)
" on the return type of the getItem
method. Any idea why? If I change the return type to FriendsFragment
, I get the same error. If I change the return type to Fragment then I get errors inside the method. Please help! Thanks in advanced. Here is my code:
package com.erinkabbash.ribbit;
import java.util.Locale;
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.Context;
import android.support.v13.app.FragmentPagerAdapter;
import android.support.v4.app.ListFragment;
/**
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
protected Context mContext;
public SectionsPagerAdapter(Context context, FragmentManager fm) {
super(fm);
mContext = context;
}
@Override
public InboxFragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a PlaceholderFragment (defined as a static inner class
// below).
switch(position){
case 0:
return new InboxFragment();
case 1:
return new FriendsFragment();
}
return null;
}
@Override
public int getCount() {
return 2;
}
@Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return mContext.getString(R.string.title_section1).toUpperCase(l);
case 1:
return mContext.getString(R.string.title_section2).toUpperCase(l);
}
return null;
}
}
Upvotes: 0
Views: 147
Reputation: 771
Is your class InboxFragment and FriendsFragment extends Fragment(android.support.v4.app.Fragment) ?
android.support.v4.app.Fragment
and android.app.Fragment
are not compatible to each other and FragmentPagerAdapter supports android.support.v4.app.Fragment
only.
Give it a try, it should work..
Upvotes: 1
Reputation: 26198
You are returning a type InboxFragment
and int the line of code return new FriendsFragment();
it wont accept it because it is not a type of InboxFragment
but it is a type of Fragment
so instead of returning InboxFragment
from the getItem
method change it to Fragment
example:
public Fragment getItem(int position) {
Upvotes: 1
Reputation: 93559
You can't do an override unless the signature matches exactly. Change getItem from returning InboxFragment to returning Fragment.
Upvotes: 3