Android Studio and android.support.v4.app.Fragment

2019-01-13 19:43发布

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 =)

19条回答
够拽才男人
2楼-- · 2019-01-13 20:20

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.

查看更多
别忘想泡老子
3楼-- · 2019-01-13 20:22

For me : Build->Clean Project solved this question

查看更多
一夜七次
4楼-- · 2019-01-13 20:23

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.

查看更多
混吃等死
5楼-- · 2019-01-13 20:23

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

查看更多
爷的心禁止访问
6楼-- · 2019-01-13 20:24

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.

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

查看更多
Ridiculous、
7楼-- · 2019-01-13 20:24

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.

查看更多
登录 后发表回答