Android,无法转换为Fragment

时间:2018-08-21 07:19:38

标签: android android-fragments fragment fragmentpageradapter support-v4

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

还在build.gradle(应用程序)中添加了数据包也无法正常工作..

我的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(app)

        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'

3 个答案:

答案 0 :(得分:0)

使用getFragmentMangager()时,请确保您的Fragment类扩展了android.app.Fragment类。 如果您有任何机会使用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;

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

您的进口商品似乎发生冲突...

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,以便导入成为已知。