Android,无法转为Fragment

Android, cannot be converted to Fragment

我想为我的聊天编写片段,但我总是收到相同的错误消息。在互联网上,我读到它应该与 "import" 和 "import android.support.v4.app.Fragment;" 有关,但这对我不起作用。

添加的数据包也在 build.gradle (app) 中不起作用..

我的 Android Studio 版本是 3.3 Canary 5。

希望你能解决我的问题,非常感谢!

TabsPagerAdapter

    import androidx.fragment.app.Fragment;
    import androidx.fragment.app.FragmentManager;
    import androidx.fragment.app.FragmentPagerAdapter;

    import android.support.v4.app.Fragment;

    class TabsPagerAdapter extends FragmentPagerAdapter {

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


    @Override
    public Fragment getItem(int position) {

        switch (position) {
            case 0:
                RequestsFragment requestsFragment = new RequestsFragment();
                return requestsFragment;

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

build.gradle(应用程序)

        apply plugin: 'com.android.application'

    android {
        compileSdkVersion 28
        buildToolsVersion "28.0.2"
        defaultConfig {
            applicationId "com.nexsis.entwicklung.swapv12"
            minSdkVersion 15
            targetSdkVersion 28
            versionCode 1
            versionName "1.0"
            testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        }
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
            }
        }
    }


    dependencies {

        implementation 'com.android.support:support-v4:28'
        implementation 'com.android.support:appcompat-v7:28.0.0-alpha1'
        implementation 'com.android.support:recyclerview-v7:28.0.0-alpha1'

        implementation 'com.google.firebase:firebase-auth:16.0.2'
        implementation 'com.google.firebase:firebase-core:16.0.0'
        implementation fileTree(dir: 'libs', include: ['*.jar'])
        implementation 'com.android.support:appcompat-v7:28'
        implementation 'com.android.support:design:28'
        implementation 'android.arch.navigation:navigation-fragment:1.0.0-alpha04'
        implementation 'androidx.constraintlayout:constraintlayout:1.1.2'
        testImplementation 'junit:junit:4.12'
        androidTestImplementation 'androidx.test:runner:1.1.0-alpha4'
        androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0-alpha4'
    }
    apply plugin: 'com.google.gms.google-services'

使用 getFragmentMangager() 时,请确保您的 Fragment class 扩展 android.app.Fragment class。 如果您有任何机会使用 android.support.v4.app.Fragment (请参阅您的导入),那么您需要使用 getSupportFragmentManager() 而不是

将此添加到您的 build.gradle

 implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support:design:27.1.0'

你的导入应该是

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

而不是

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;

尝试对 Fragment、FragmentManager、FragmentPagerAdapter 使用相同的版本

import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.Fragment;

您的导入似乎发生冲突...

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;

// this one is the old version of androidx.fragment.app.Fragment
// import android.support.v4.app.Fragment;

降级,正如其他答案所暗示的那样,似乎不是解决方案。 请参阅 artifact mappings,其中解释了如何获得 build.gradle;例如,com.android.support:support-fragment 需要用 androidx.fragment:fragment 替换,这样导入才会为人所知。