Android Studio 和 android.support.v4.app.Fragment:无法解析符号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20386331/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 02:47:05  来源:igfitidea点击:

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

androidandroid-fragmentsandroid-studio

提问by Ted

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.

我正在尝试使用 Android Studio 而不是 Eclipse。我安装它,然后启动一个全新的项目并按照向导进行操作。我没有添加自己的代码。

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

然后,我右键单击以创建一个新组件,一个新 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:

...所以我开始使用谷歌搜索并发现我需要安装和引用支持库 4,我这样做了。当我检查 build.gradle(不管那是什么,对我来说是来自 Eclipse 的新手)时,我看到:

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:

因为他们在这里这么说。经过重新编译和所有这些,错误仍然存​​在。然后我直接引用 .jar 文件,如下所示:

enter image description here

在此处输入图片说明

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

并再次重新编译 yada yada,但这也无济于事。

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

这种行为对我来说似乎很奇怪。我在这里缺少什么?这是 SDK 管理器视图:

enter image description here

在此处输入图片说明

So, what am I missing? Thanks =)

那么,我错过了什么?谢谢=)

回答by Owen Chen

DID NOT WORK:

不工作:

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

我的 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
  • 使缓存无效/重新启动
  • 将项目与 gradle 文件同步
  • 清洁工程
  • 重建项目
  • 渐变干净

But, none of them worked for me.

但是,他们都没有为我工作。

SOLUTION:

解决方案:

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

最后,我通过删除 "/.idea/libraries"解决了它,然后与 gradle 同步并再次构建。

回答by Scott Barta

The symptom of this problem is usually that the build works fine from the command line (which means your build.gradlefile is set up right) but you get syntax highlighting errors in the IDE. Follow This Steps To Solve The Problem: Click on Toolsfrom the toolbar usually at the top part of your IDE, and then navigate to Androidthen navigate to Sync Project with Gradle Filesbutton. 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

此问题的症状通常是从命令行构建工作正常(这意味着您的build.gradle文件设置正确),但在 IDE 中出现语法高亮错误。按照以下步骤解决问题:单击工具栏中通常位于 IDE 顶部的工具,然后导航到Android,然后导航到Sync Project with Gradle Files按钮。我们意识到 IDE 不能只顾自己而不是强迫您在正确的时间手动同步是不太理想的;我们正在https://code.google.com/p/android/issues/detail?id=63151 中跟踪这方面的进展

回答by Suresh Parmar

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

试试这可能会帮助你。转到“文件”->“使缓存无效...”,然后选择“无效并重新启动”选项来解决这个问题。

回答by Rohit Mandiwal

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

“文件”->“使缓存无效...”

worked for me.

为我工作。

回答by Lam Do

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

自从我更新到最新版本的 Android Studio 0.3.7 以来,我遇到了同样的问题。所以你可以试试我的东西。

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

确保您已更新到最新版本的 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

正如上面的附件图片,你已经做到了。然后将以下设置添加到您的 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

还有一件事:请确保您的 Android SDK 定位到正确的 SDK 文件夹

回答by Pedro Gonzalez

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.

在此处输入图片说明我找到了一个快捷方式:文件 - 项目结构 - 选项卡:依赖项 单击绿色 + 符号,选择 support-v4(或任何其他您需要的),单击确定。

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

现在转到您的 gradle 文件并查看已添加

回答by user5259011

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

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

its work for me

它对我有用

回答by Nathania Sutedja

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

我正在使用 buck ,似乎通过删除/.idea/libraries项目文件夹中的 并同步 gradle 再次对我有用。

回答by raddevus

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 :

我正在运行迄今为止最新版本的 AndroidStudio(2015 年 11 月 10 日)——v1.4(构建 AI-141.2288178,构建于 2015 年 9 月 28 日)并且我构建了我的项目并且一切正常。然后在我的计算机处于非活动状态几个小时后,我回来了,编辑了一些与支持库无关的代码并开始看到:

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

无法解析符号“fragmentactivity”,我在 AndroidStudio 编辑器中看到与 fragmentactivity 相关的红色突出显示项目。

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.

我只是提到这一点,所以其他人可能会看到它并知道它正在发生。

回答by Divyanshu Kumar

replace this line of code

替换这行代码

import android.support.v4.app.FragmentActivity;

with

import androidx.fragment.app.FragmentActivity;

and you are done.

你就完成了。