Android, cannot be converted to Fragment

8.3k Views Asked by At

I wanted to program fragments for my chat but I always get the same error message. on the internet, I read that it should have to do with the "import" and the "import android.support.v4.app.Fragment;" but that does not work for me.

Added the packet also in the build.gradle (app) does not work ..

My Android Studio version is 3.3 Canary 5.

I hope you can solve my problem maybe, many thanks!

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

There are 3 best solutions below

2
Martin Zeitler On BEST ANSWER

your imports seem to collide ...

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;

down-grading, as other answers suggest does not appear to be the solution. see the artifact mappings, which explain how to chance the build.gradle; for example, com.android.support:support-fragment would need to be replaced there with androidx.fragment:fragment, so that the import would become known.

1
Sandeep Parish On

When using getFragmentMangager(), make sure that your Fragment classes extend android.app.Fragment class. If by any chance you are using android.support.v4.app.Fragment (see your imports), then you need to use getSupportFragmentManager() instead

add this in your build.gradle

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

and your import should be

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

instead of

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
4
Parul On

Try to use same version for Fragment,FragmentManager,FragmentPagerAdapter

i.e.

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