[android] Android Studio and android.support.v4.app.Fragment: cannot resolve symbol

I am tryng out Android Studio instead of Eclipse. I install it and then start a completely new project and follow the wizard. I add no code of my own.

Then, I right-click to create a new component, a new Fragment:

enter image description here

and choose a new fragment:

enter image description here

and as soon as I do, I see compile errors:

enter image description here

... so I start googling and find out that I need to install and reference support library 4, and that I do. When I check the build.gradle (whatver that is, new to me coming from Eclipse), I see:

enter image description here

but I change that to

enter image description here

because they said so here. After recompiling and all that, the error is still there. I then reference the .jar-file directly, like this:

enter image description here

and again do recompile yada yada, but that doesnt help either.

This behaviour seems very strange to me. What am I missing here? This is the SDK Manager view:

enter image description here

So, what am I missing? Thanks =)

This question is related to android android-fragments android-studio

The answer is


File -> "Invalidate caches/Restart" This worked for me.


Try this may will help you.Go to "File" -> "Invalidate Caches...", and select "Invalidate and Restart" option to fix this.


I'm running the most current version of AndroidStudio to date (11/10/2015) -- v1.4 (build AI-141.2288178, built on September 28, 2015) and I built my project and everything worked fine. Then after a few hours of my computer being inactive I came back, edited some code that had nothing to do with the support libraries and started seeing :

cannot resolve symbol 'fragmentactivity' and I was seeing red highlighted items related to fragmentactivity in the AndroidStudio editor.

The solution was to simply do a

Build...Rebuild Project...

I only mention this so others might see it and know it is happening out there.


I was facing the same issue. As it turns out in my build.gradle file there was this :

configurations {
    all*.exclude group: 'com.android.support'
}

Removing that fixed my issue. So guys, if doing all that and still your issue is not fixed, look for any exclude keywords in your gradle file.


I encountered this issue and tried everything including File > Invalidate Caches but nothing worked. The reason this issue was happening for me was because I had external projects that were using a different AppCompat version to my main gradle file.

After I updated all gradle files to be the same version the compile error went away.


I'm using buck and it seems like by removing the /.idea/libraries in your project folder and sync gradle again works for me.


I found that if you have comments in your build.gradle it may break when you try to add a new support library. So make sure you check your build.gradle and see if it looks alright manually.


For everyone who comes to this thread and builds with maven: add support-v4 dependency and build to download the repo.

Then add the library manuelly to your project:

Project Structure -> Libraries -> Add

And move to you're repo for the correct jar file: C:\Users\XXXXXXX\.m2\repository\com\google\android\support-v4\r7\support-v4-r7.jar


Do sth. modification (just to gradle sync) over app level build.gradle and sync. Again, redo what you changed in build.gradle and sync. It should fix your problem.


For me : Build->Clean Project solved this question


"File" -> "Invalidate Caches..."

worked for me.


Android studio has option to manage dependencies. Follow path.

  1. Click on File, then select Project Structure
  2. Choose Modules "app"
  3. Click "Dependencies" tab
  4. Click on the + sign, choose Library Dependencies
  5. Select support-v4 or other libraries as needed and click OK

FYI check link stackoverflow.com/a/33414287/1280397


DID NOT WORK:

I have already had the following dependency in my build.gradle

implementation 'com.android.support:support-v13:26.0.2'

I have tried all of the following,

  • Invalidate Caches/Restart
  • Sync project with gradle files
  • Clean project
  • Rebuild project
  • gradlew clean

But, none of them worked for me.

SOLUTION:

Finally, I solved it by deleting "/.idea/libraries", and then synced with gradle and built again.


For me the cause of problem was broken class path:

Library Gradle: com.android.support:support-v4:28.0.0@aar has broken classes path:   
/Users/YOUR_USER/.gradle/caches/transforms-1/files-1.1/support-v4-28.0.0.aar/0f378acce70d3d38db494e7ae5aa6008/res

So only removing transforms-1 folder and further Gradle Sync helped.


Closing and Re-Importing project worked for me.


replace this line of code

import android.support.v4.app.FragmentActivity;

with

import androidx.fragment.app.FragmentActivity;

and you are done.


To downplay the "magic" of this issue a little bit. You need an Internet connection after you make those changes. If for some reason required libraries cannot by downloaded, instead of giving an appropriate message (like "Failed to fetch libraries") you will simply get the same build error. Was struggling with this for an hour before realizing that my company's VPN blocked the repo.


Hrrm... I dont know how many times this has happend so far: I test, try, google, test again and mess around for hours, and when I finally give up, I go to my trusted Stackoverflow and write a detailed and clear question.

When I post the question, switch over to the IDE and boom - error gone.

I can't say why its gone, because I change absolutely nothing in the code except for that I already tried as stated above. But all of a sudden, the compile error is gone!

In the build.gradle, it now says:

dependencies {
    compile "com.android.support:appcompat-v7:18.0.+"
}

which initially did not work, the compile errors did not go away. it took like 30 min before the IDE got it, it seems... hmm...

=== EDIT === When I view the build.gradle again, it has now changed and looks like this:

dependencies {
    compile 'com.android.support:support-v4:18.0.0'
    compile "com.android.support:appcompat-v7:18.0.+"
}

Im not really sure what the appcompat is right now.


I got the same problem since I updated to latest version of Android Studio 0.3.7. So you can try with my stuffs.

Ensure you have updated to latest version Android Support Repository - 3 Android Support Library - 19

As your attachment picture above, you did it already. Then adding the following setting to your build.gradle

dependencies {
    compile 'com.android.support:support-v4:19.0.0'
}

One more thing: Please make sure your Android SDK is targeting to right SDK folder


1> File -> invalidate caches 2> Build-> Rebuild

its work for me


enter image description hereI found a shortcut: File - Project Structure - Tab:Dependencies Click on the green + sign, select support-v4 (or any other you need), click OK.

now go to your gradle file and see that is been added


Examples related to android

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How to implement a simple scenario the OO way My eclipse won't open, i download the bundle pack it keeps saying error log getting " (1) no such column: _id10 " error java doesn't run if structure inside of onclick listener Cannot retrieve string(s) from preferences (settings) strange error in my Animation Drawable how to put image in a bundle and pass it to another activity FragmentActivity to Fragment A failure occurred while executing com.android.build.gradle.internal.tasks

Examples related to android-fragments

FragmentActivity to Fragment How to start Fragment from an Activity How to use data-binding with Fragment In android how to set navigation drawer header image and name programmatically in class file? Android Fragment onAttach() deprecated How to convert any Object to String? Activity, AppCompatActivity, FragmentActivity, and ActionBarActivity: When to Use Which? Difference and uses of onCreate(), onCreateView() and onActivityCreated() in fragments java.lang.IllegalStateException: Fragment not attached to Activity java.lang.NullPointerException: Attempt to invoke virtual method 'int android.view.View.getImportantForAccessibility()' on a null object reference

Examples related to android-studio

A failure occurred while executing com.android.build.gradle.internal.tasks "Failed to install the following Android SDK packages as some licences have not been accepted" error Android Gradle 5.0 Update:Cause: org.jetbrains.plugins.gradle.tooling.util This version of Android Studio cannot open this project, please retry with Android Studio 3.4 or newer WARNING: API 'variant.getJavaCompile()' is obsolete and has been replaced with 'variant.getJavaCompileProvider()' Flutter plugin not installed error;. When running flutter doctor ADB.exe is obsolete and has serious performance problems Android design support library for API 28 (P) not working Flutter command not found How to find the path of Flutter SDK