片段错误:不兼容的类型必需:android.support.v4.app.Fragment,Found:package_name.app_name.Fragment_name

时间:2017-10-14 08:51:27

标签: java android fragment required

我遇到以下代码的不兼容类型错误:

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

见下图:

click here to see the screenshot

任何人都可以帮助我摆脱这个错误吗?以及如何解决它。

1 个答案:

答案 0 :(得分:2)

让您的课程RequestsFragment从课程Fragment扩展课程android.support.v4.app

如果您已经扩展了Fragment类,请检查import语句并确保

import android.support.v4.app.Fragment;

而不是

import android.app.Fragment;