Java 导入android.support无法解析

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18299898/
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-12 00:15:12  来源:igfitidea点击:

The import android.support cannot be resolved

javaandroidpackagesrss-reader

提问by user13267

I am trying to run the code provided HERE
I downloaded the code from their Github and imported into Android SDK, but it shows error at the lines

我正在尝试运行这里提供的代码
我从他们的 Github 下载了代码并导入到 Android SDK 中,但它在行中显示错误

import android.support.v4.app.FragmentActivity;
import android.support.v4.app.NavUtils;  

in multiple files. However if I check the Android SDK Manager, the Android Support Libraryas well as the Android Support Repositoryin the Extras folder are both installed. I can also see the support folder and it's contents in the SDK_INSTALL\sdk\extras\android\supportdirectory. It also has the v4 folder which also contains the android-support-v4.jarand the srcfolder. THen why is it still showing the error, how do I resolve it and how do I get that sample code running? THe sample code is for an RSS reader app, if that's relevant.

在多个文件中。但是,如果我检查了Android SDK管理器中,Android Support Library以及在Android Support Repository在都安装文件夹中的附加功能。我还可以看到支持文件夹及其目录中的内容SDK_INSTALL\sdk\extras\android\support。它还具有 v4 文件夹,其中还包含android-support-v4.jarsrc文件夹。那么为什么它仍然显示错误,我该如何解决它以及如何运行该示例代码?示例代码适用于 RSS 阅读器应用程序(如果相关)。

采纳答案by Salman Khakwani

Please follow these Steps:

请按照以下步骤操作:

For Eclipse:

对于日食:

  • Go to your Project's Properties
  • Navigate to the Java Build Path
  • Then go to the Librariestab. There click the Add External JARsButton on the Right pane.
  • Select the android-support-v4.jarfile, usually the path for the Jar fileis :
    YOUR_DRIVE\android-sdks\extras\android\support\v4\android-support-v4.jar
  • After adding android-support-v4.jarLibrary, navigate to the Order and Exporttab and put check mark on the android-support-v4Library file.
  • After doing the above, Cleanthe Project and Buildit.
  • Problem Solved.
  • 转到您的项目 Properties
  • 导航到 Java Build Path
  • 然后转到Libraries选项卡。单击Add External JARs右侧窗格上的按钮。
  • 选择android-support-v4.jar文件,通常jar文件的路径是:
    YOUR_DRIVE\android-sdks\extras\android\support\v4\android-support-v4.jar
  • 添加android-support-v4.jar库后,导航到Order and Export选项卡并在android-support-v4库文件上打勾。
  • 完成上述操作后,清理项目并构建它。
  • 问题解决了。

For Android Studio:

对于安卓工作室:

Short Version:

精简版:

  • Add the following line to your build.gradlefile:
    implementation 'com.android.support:support-v4:YOUR_TARGET_VERSION'
  • 将以下行添加到您的build.gradle文件中:
    implementation 'com.android.support:support-v4:YOUR_TARGET_VERSION'

Long Version:

长版:

  • Go to File -> Project Structure

  • Go to "Dependencies" Tab -> Click on the Plus sign -> Go to "Library dependency"

  • Select the support library "support-v4 (com.android.support:support-v4:YOUR_TARGET_VERSION)"

  • Navigate to your "build.gradle" inside your App Directory and double check if your desired Android Support Library has been added to your dependencies.

  • Rebuild your project and now everything should work.

  • 转到文件 -> 项目结构

  • 转到“依赖项”选项卡 -> 单击加号 -> 转到“库依赖项”

  • 选择支持库“support-v4 (com.android.support:support-v4:YOUR_TARGET_VERSION)”

  • 导航到您的应用程序目录中的“build.gradle”,并仔细检查您想要的 Android 支持库是否已添加到您的依赖项中。

  • 重建您的项目,现在一切正常。

Further reading regarding this Question:

关于这个问题的进一步阅读:

  1. Support Library - Android Dev
  2. Recent Support Library Revisions
  3. Support Library Packages
  4. What is an Android Support Library?
  5. How Android Support Library work?
  1. 支持库 - Android 开发
  2. 最近的支持库修订
  3. 支持库包
  4. 什么是 Android 支持库?
  5. Android 支持库如何工作?

I hope this helps.

我希望这有帮助。

回答by Dexter

Another way to solve the issue:

解决问题的另一种方法:

If you are using the support library, you need to add the appcompatlib to the project. This linkshows how to add the support lib to your project.

如果您使用的是支持库,则需要将库添加appcompat到项目中。此链接显示了如何将支持库添加到您的项目中。

Assuming you have added the support lib earlier but you are getting the mentioned issue, you can follow the steps below to fix that.

假设您之前添加了支持库,但遇到了上述问题,您可以按照以下步骤进行修复。

  1. Right click on the project and navigate to Build Path > Configure Build Path.

  2. On the left side of the window, select Android. You will see something like this:

  1. 右键单击项目并导航到Build Path > Configure Build Path

  2. 在窗口左侧,选择Android。你会看到这样的事情:

enter image description here

在此处输入图片说明

  1. You can notice that no library is referenced at the moment. Now click on the Addbutton shown at the bottom-right side. You will see a pop up window as shown below.
  1. 您可以注意到目前没有引用任何库。现在单击右下角显示的添加按钮。您将看到一个弹出窗口,如下所示。

enter image description here

在此处输入图片说明

  1. Select the appcompatlib and press OK. (Note: The lib will be shown if you have added them as mentioned earlier). Now you will see the following window:
  1. 选择appcompat库并按OK。(注意:如果您如前所述添加了它们,则会显示该库)。现在您将看到以下窗口:

enter image description here

在此处输入图片说明

  1. Press OK. That's it. The lib is now added to your project (notice the red mark) and the errors relating inclusion of support lib must be gone.
  1. 确定。就是这样。该库现在已添加到您的项目中(注意红色标记)并且与包含支持库相关的错误必须消失。

回答by Abdul Rizwan

This is very easy step to import any 3rd party lib or jar file into your project

这是将任何 3rd 方 lib 或 jar 文件导入项目的非常简单的步骤

  1. Copy android-support-v4.jarfile from your_drive\android-sdks\extras\android\support\v4\android-support-v4.jar
    orcopy from your existing project's bin folder.
    or any third party .jar file
  2. paste copied jarfile into libfolder

  3. right click on this jar file and then click on build Path->Add to Build Pathenter image description here

  4. even still you are getting error in your project then Clean the Project and Build it.

  1. 复制Android的支持,v4.jar文件从 了Your_Drive \ Android的软件开发工具包\演员\机器人\ SUPPORT \ V4 \ Android的支持,v4.jar
    从现有项目的bin文件夹复制。
    或任何第三方 .jar 文件
  2. 将复制的jar文件粘贴到lib文件夹中

  3. 右键单击这个jar文件,然后单击构建路径->添加到构建路径在此处输入图片说明

  4. 即使您仍然在项目中出错,然后清理项目并构建它。

回答by Arun Badole

This issue may also occur if you have multiple versions of the same support library android-support-v4.jar. If your project is using other library projects that contain different-2 versions of the support library. To resolve the issue keep the same version of support library at each place.

如果您有多个版本的同一个支持库,也可能会出现此问题android-support-v4.jar。如果您的项目正在使用包含不同 2 版本支持库的其他库项目。要解决此问题,请在每个位置保持相同版本的支持库。

回答by Jawad Zeb

I have resolved it by deleting android-support-v4.jarfrom my Project. Because appcompat_v7already have a copy of it.

我已经通过android-support-v4.jar从我的项目中删除来解决它。因为appcompat_v7已经有副本了。

If you have already import appcompat_v7 but still the problem doesn't solve. then try it.

如果您已经导入了 appcompat_v7 但问题仍然没有解决。然后尝试一下。

回答by Gene

andorid-support-v4.jar is an external jar file that you have to import into your project.

andorid-support-v4.jar 是一个外部 jar 文件,您必须将其导入到您的项目中。

This is how you do it in Android Studio:

这是您在 Android Studio 中的操作方式:

Go to File -> Project Structure enter image description here

转到文件 -> 项目结构 在此处输入图片说明

Go to "Dependencies" Tab -> Click on the Plus sign -> Go to "Library dependency" enter image description here

转到“依赖项”选项卡 -> 单击加号 -> 转到“库依赖项” 在此处输入图片说明

Select the support library "support-v4 (com.android.support:support-v4:23.0.1)" enter image description here

选择支持库“support-v4 (com.android.support:support-v4:23.0.1)” 在此处输入图片说明

Now to go your "build.gradle" file in your app and make sure the android support library has been added to your dependencies. Alternatively, you could've also just typed compile 'com.android.support:support-v4:23.0.1'directly into your dependencies{}instead of doing it through the GUI.

现在转到您的应用程序中的“build.gradle”文件,并确保已将 android 支持库添加到您的依赖项中。或者,您也可以 compile 'com.android.support:support-v4:23.0.1'直接输入,dependencies{}而不是通过 GUI 输入。

enter image description here

在此处输入图片说明

Rebuild your project and now everything should work. enter image description here

重建您的项目,现在一切正常。 在此处输入图片说明

回答by Wayferer

I followed the instructions above by Gene in Android Studio 1.5.1 but it added this to my build.gradle file:

我在 Android Studio 1.5.1 中按照 Gene 上面的说明进行了操作,但它已将其添加到我的 build.gradle 文件中:

compile 'platforms:android:android-support-v4:23.1.1'

so I changed it to:

所以我把它改成:

compile 'com.android.support:support-v4:23.1.1'

And it started working.

它开始工作了。

回答by JesseBoyd

Android Studio 2.2.3 Linux Mint 18.1

Android Studio 2.2.3 Linux Mint 18.1

Inside your 'project view' open Gradle Scripts -> build.gradle(Module:app) and put your mouse pointer inside the word dependencies.

在您的“项目视图”中打开 Gradle Scripts -> build.gradle(Module:app) 并将鼠标指针放在单词依赖项内。

Click on the light bulb and click "add library dependency" and for me all the libraries I wanted were listed there.

单击灯泡并单击“添加库依赖项”,我想要的所有库都列在那里。

example libraries that came up for me: compile 'com.android.support:gridlayout-v7:25.1.0' compile 'com.android.support:support-v13:25.1.0'

为我提供的示例库: compile 'com.android.support:gridlayout-v7:25.1.0' compile 'com.android.support:support-v13:25.1.0'

I am now looking to add android support by default in Gradles default configuration.

我现在希望在 Gradles 默认配置中默认添加 android 支持。

回答by Ajji

For me they were appearing when i transferred code manually to another laptop. Just do

对我来说,当我手动将代码传输到另一台笔记本电脑时,它们就会出现。做就是了

File>Invalidate Cache/Restart

文件>无效缓存/重启

click on 'Invalidate Cache and Restart' and your are done.

单击“使缓存无效并重新启动”,您就完成了。