1

I tried creating an Android app. using tabs in Android Studio. I ran into the issue where all my fragment imports were causing the error Cannot Resolve Symbol 'FragmentManager', and anything else associated with fragments within my program. I made sure I was importing the correct support files:

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

Image of Errors

I checked to make sure I was calling active fragments in my layout files, similar issue explained [here].

I made sure I had the latest Android SDK Build-tools, shown [here].

I [re-synced the Gradle Files].

    Tools > Android > Sync Project with Gradle Files

I even imported a pre-made application from the Android Developer guides, [Creating Swipe Views with Tabs], and the error was not going away. Which made me think that there was possibly something wrong with Android Studio. I recently updated to Version 0.8.14 and I noticed the file structure changed a bit, it is now more streamlined.

Application Structure

Which lead me back to poke around within the gradle files. I opened up the build.gradle and verified that there were no issues from the minSdk version and then did one last check on possible errors with Gradle.

apply plugin: 'com.android.application'

android {
    compileSdkVersion 15
    buildToolsVersion "20.0.0"

    defaultConfig {
        applicationId "app.name"
        minSdkVersion 15
        targetSdkVersion 15
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
}

Check out my answer for the [solution].

Community
  • 1
  • 1
Black Bird
  • 797
  • 1
  • 10
  • 34

3 Answers3

3

While this was not my question, I stumbled upon [this] thread, that suggests that you add other dependencies to the build for the support build library. Add this line within the dependencies section:

compile 'com.android.support:support-v4:18.0.0'

(For more clarification, using the previous code, you would add this beneath the compile fileTree(dir: 'libs', include: ['*.jar']).

Once you've added this, Android Studio would point out the gradle files have changed and that you should re-sync them. Once the sync is complete, it should resolve the not being able to find the fragments!

Gradle Sync

Community
  • 1
  • 1
Black Bird
  • 797
  • 1
  • 10
  • 34
2

UPDATE:

For those facing this error in June 2019 upwards, make sure that if you are using the now new androidX library, you must make change android.support.v4.app.FragmentManager anywhere you call the library in your code to androidx.fragment.app.FragmentManager

Samuel Ewudzi Incoom
  • 1,802
  • 17
  • 17
1

For those of you stumbling across this now you may be interested to know that the support library had a split in version 24.2.0 (August 2016):

Note: 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. However, to reduce APK size, we recommend that you just list the specific modules your app needs.

v4 compat library

com.android.support:support-compat:24.2.0

v4 core-utils library

com.android.support:support-core-utils:24.2.0

v4 core-ui library

com.android.support:support-core-ui:24.2.0

v4 media-compat library

com.android.support:support-media-compat:24.2.0

v4 fragment library

com.android.support:support-fragment:24.2.0

Reference:

Developer.Android.com

Chad Bingham
  • 32,650
  • 19
  • 86
  • 115