简体   繁体   中英

Fragment Error: Incompatible Types Required: android.support.v4.app.Fragment, Found: package_name.app_name.Fragment_name

I got an incompatible type error for the below code:

class TabsPagerAdapter extends FragmentPagerAdapter {

public TabsPagerAdapter(FragmentManager fm) {
    super(fm);
}

@Override
public Fragment getItem(int i) {
    switch (i) {
        case 0:
            RequestsFragment requestFragment = new RequestsFragment();
            return requestFragment;
        case 1:
            ChatsFragment chatsFragment = new ChatsFragment();
            return chatsFragment;
        case 2:
            FriendsFragment friendsFragment = new FriendsFragment();
            return friendsFragment;
        default:
            return null;
    }
}
@Override
public int getCount () {
    return 3;
}

public CharSequence getPageTitle(int position) {
    switch (position) {
        case 0:
            return "Requests";
        case 1:
            return "Chats";
        case 2:
            return "Friends";
        default:
            return null;
    }
}
}

See the image below:

点击此处查看屏幕截图

Can anyone please help me to get out from this error? And how to resolve it.

Make your class RequestsFragment extend the class Fragment from the package android.support.v4.app .

If you have already extended the Fragment class, check the import statements and make sure there is

import android.support.v4.app.Fragment;

and not

import android.app.Fragment;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM