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:26

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.

查看更多
啃猪蹄的小仙女
3楼-- · 2019-01-13 20:28

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

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

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

查看更多
▲ chillily
5楼-- · 2019-01-13 20:31

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

查看更多
对你真心纯属浪费
6楼-- · 2019-01-13 20:31

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.

查看更多
SAY GOODBYE
7楼-- · 2019-01-13 20:31

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

查看更多
登录 后发表回答