Can't Import android.support.v4.app.Fragment but can import FragmentManager

Viewed 8563

I am trying import android.support.v4.app.Fragment but I get the error "Unresolved Reference: Fragment". I can't import it anywhere in my project. Right now I'm trying to import it into a Fragment class.

But if I try to import android.support.v4.app.FragmentManager or any other class inside android.support.v4.app it works just fine. I have tried syncing gradle, Invalidating Caches and searching the internet. I can't seem to find what might be the problem.

Here is my fragment class

package com.example.johnDoe.storeapp

import android.app.Fragment
import android.support.v4.app.Fragment // Gives the error "Unresolved Reference: Fragment"
import android.support.v4.app.FragmentManager //Works fine
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup

class StoresFragment : Fragment(){

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup, savedInstanceState: Bundle): View {
    val view: View
    view = inflater.inflate(R.layout.fragment_stores, container, false)



    return view
}
}

This is my build.gradle file

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.example.johnDoe.storeapp"
        minSdkVersion 15
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support:design:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
2 Answers

If anyone is still having this problem shift to AndroidX, but make sure you are on or update your project to use the final version of the support library: version 28.0.0.

open refactor from the menu bar in android studio and click migrate to AndroidX. usually, all the imports will be updated else use these imports:

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

and that should solve it else move the cursor onto error and the Android studio will prompt to import class, import and that should do it.

I believe you explicitely need the com.android.support:support-fragment:28.0.0 dependency (since the support-v4 library got split up) or just include implementation 'com.android.support:support-v4:28.0.0' to include everything from support-v4.

From https://developer.android.com/topic/libraries/support-library/packages#v4:

Prior to Support Library revision 24.2.0, there was a single v4 support library. That library was divided into multiple modules to improve efficiency.

For backwards compatibility, if you list support-v4 in your Gradle script, your APK will include all of the v4 modules.

Although nowadays I use AndroidX.

Related