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'
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With