Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

android

kotlin

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'
}
like image 376
28Flex Avatar asked Oct 20 '25 14:10

28Flex


1 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.

like image 132
Arjun.ts Avatar answered Oct 22 '25 04:10

Arjun.ts