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条回答
【Aperson】
2楼-- · 2019-01-13 20:14

The symptom of this problem is usually that the build works fine from the command line (which means your build.gradle file is set up right) but you get syntax highlighting errors in the IDE. Follow This Steps To Solve The Problem: Click on Tools from the toolbar usually at the top part of your IDE, and then navigate to Android then navigate to Sync Project with Gradle Files button. We realize it's less than ideal that the IDE can't just take care of itself instead of forcing you to manually sync at the right time; we're tracking progress on this in https://code.google.com/p/android/issues/detail?id=63151

查看更多
一纸荒年 Trace。
3楼-- · 2019-01-13 20:15

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

worked for me.

查看更多
来,给爷笑一个
4楼-- · 2019-01-13 20:15

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.

查看更多
Animai°情兽
5楼-- · 2019-01-13 20:16

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

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

Complementing the @daylight answer...

goto layout, if your layout don't show, will show a message with

"Tip: build your project"

. So build your project and everything will be fine.

查看更多
闹够了就滚
7楼-- · 2019-01-13 20:18

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

its work for me

查看更多
登录 后发表回答