java 在Android Studio中标记为错误代码,但成功构建gradle并在设备上运行

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

Marked as an error code in Android Studio, but successfully build gradle and run on the device

javaandroidandroid-studio

提问by Amay Diam

i have problem with my code. My Code marked as an error code in Android Studio, but successfully build gradle and run on the device, like this : enter image description here

我的代码有问题。我的代码在 Android Studio 中标记为错误代码,但成功构建 gradle 并在设备上运行,如下所示: 在此处输入图片说明

current configure:

当前配置:

  1. My current project uses a lot of libraries in dependencies. But if the project with little library will not be marked errors like this.
  2. Android Studio 1.5 RC1
  3. compileSdkVersion 'Google Inc.:Google APIs:23
  4. buildToolsVersion '23.0.2'
  5. classpath 'com.android.tools.build:gradle:1.3.1'
  1. 我当前的项目在依赖项中使用了很多库。但是如果库很少的项目就不会出现这样的标记错误了。
  2. 安卓工作室 1.5 RC1
  3. compileSdkVersion 'Google Inc.:Google APIs:23
  4. buildToolsVersion '23.0.2'
  5. 类路径 'com.android.tools.build:gradle:1.3.1'

activity I have done but have not solved the problem:

我已完成但尚未解决问题的活动:

  1. always clean > re-build project
  2. invalidate caches / restart > open Android studio > open this project
  3. Close android studio > restart computer > open again android studio > open this project
  4. Update Android version and SDK version 5.Uninstall Android Studio > Re-Install Android Studio > Open Project
  1. 始终清洁 > 重新构建项目
  2. 使缓存无效/重新启动 > 打开 Android Studio > 打开这个项目
  3. 关闭android studio > 重启电脑 > 再次打开android studio > 打开这个项目
  4. 更新 Android 版本和 SDK 版本 5.Uninstall Android Studio > Re-Install Android Studio > Open Project

edited:it's seems error each code from android support like recyclerview, CardView, APpCompact

编辑:来自android支持的每个代码似乎都是错误的,比如recyclerview、CardView、APpCompact

this my depedencies

这是我的依赖

dependencies {
    compile project(':android-crop')
    compile project(':RTEditor-Toolbar')
    compile files('libs/apache-mime4j-0.6.jar')
    compile files('libs/httpmime-4.1.3.jar')
    compile files('libs/jetbrains-annotations.jar')
    compile files('libs/gcm.jar')
    //three party library
    compile('com.crashlytics.sdk.android:crashlytics:2.5.1@aar') {
        transitive = true;
    }
    compile('cn.trinea.android.view.autoscrollviewpager:android-auto-scroll-view-pager:1.1.2') {
        exclude module: 'support-v4'
    }
    compile 'com.google.android.gms:play-services:8.3.0'
    compile 'com.squareup.picasso:picasso:2.5.2'
    compile 'com.mcxiaoke.volley:library:1.0.19'
    compile 'com.vinaysshenoy:mugen:1.0.1'
    compile 'com.github.clans:fab:1.5.5'
    compile 'com.nineoldandroids:library:2.4.0'
    compile 'com.github.curioustechizen.android-ago:library:1.3.0'
    compile 'com.jakewharton:butterknife:7.0.1'
    compile 'com.github.chrisbanes.photoview:library:+'
    compile 'com.subinkrishna:circularimageview:1.0.2'
    compile 'me.leolin:ShortcutBadger:1.1.3@aar'
    compile 'com.google.android.gms:play-services-ads:8.3.0'
    compile 'com.google.android.gms:play-services-identity:8.3.0'
    compile 'com.google.android.gms:play-services-gcm:8.3.0'
    compile 'cn.trinea.android.common:trinea-android-common:4.2.15'
    compile 'com.sothree.slidinguppanel:library:3.1.1'
    compile 'com.android.support:multidex:1.0.1'
    compile 'com.android.support:appcompat-v7:23.1.1'
    compile 'com.android.support:cardview-v7:23.1.1'
    compile 'com.android.support:design:23.1.1'
    compile 'com.android.support:recyclerview-v7:23.1.1'
    compile 'com.android.support:support-v4:23.1.1'
}

so how fix ? thanks for advance

那么如何解决?感谢提前

采纳答案by Ruchir Baronia

This is extremely common, and happens to me whenever I start my Android Studio up. Follow these steps after you have made sure that you have imported everything your code needs to import.If you are not sure how to do that, look at the very bottom of my answer, where it says More Issues that could be happening.

这是非常常见的,每当我启动我的 Android Studio 时都会发生在我身上。确保已导入代码需要导入的所有内容后,请按照以下步骤操作如果您不确定该怎么做,请查看我的答案的最底部,那里写着更多可能发生的问题

Here are some things that you can easily do to fix it:

以下是您可以轻松解决的一些问题:

  • Clean project

  • Rebuild Project

  • Restart IDE

  • Update SDK tools

  • 清洁工程

  • 重建工程

  • 重启IDE

  • 更新 SDK 工具

This is probably not an issue with your code, so you don't have to worry about it much. If cleaning and rebuilding doesn't work, just wait for some time.

这可能不是您的代码的问题,因此您不必太担心。如果清洁和重建不起作用,请等待一段时间。

Give android studio a whileto set up, and this could take a few minutes. Often times, it fixes itself without even having to do any of the steps I listed above. Make sure you do this before trying to update your SDK or messing with any files. If you do that, I suggest you take a backup first.

给 android studio 一段时间设置,这可能需要几分钟。很多时候,它甚至无需执行我上面列出的任何步骤即可自行修复。在尝试更新您的 SDK 或弄乱任何文件之前,请确保您执行此操作。如果你这样做,我建议你先备份。

I suggest you take a look at this question:

我建议你看看这个问题:

"cannot resolve symbol R" in Android Studio

Android Studio 中的“无法解析符号 R”



MORE ISSUES THAT COULD BE HAPPENING

更多可能发生的问题

Also, according to your screenshot, I saw a lot of errors in some of the same places. This could be because you are not importing the correct things!Click on one of the pieces of code that has a red squiggly line on it, and wait for a message. It should say something about imports. Then press ALT->ENTER. This should import what you need for that line of code to work.

另外,根据你的截图,我在一些相同的地方看到了很多错误。这可能是因为您没有导入正确的东西!单击上面有红色波浪线的代码段之一,然后等待消息。它应该说一些关于进口的事情。然后按ALT->ENTER。这应该导入该行代码工作所需的内容。

回答by Mike Wallstedt

If your project builds in gradle, but Android Studio displays errors, chances are that the Intellij caches are corrupt. In some cases, the menu option you already mentioned "invalidate caches / restart" is not enough. You can try the following:

如果您的项目在 gradle 中构建,但 Android Studio 显示错误,则 Intellij 缓存可能已损坏。在某些情况下,您已经提到的“使缓存无效/重新启动”的菜单选项是不够的。您可以尝试以下操作:

  1. Shutdown AS
  2. Manually delete the cache dir. On Windows, this is likely to be in a path like C:\Users\YOURNAME.AndroidStudio1.4\system\caches.
  3. Restart AS
  1. 关停AS
  2. 手动删除缓存目录。在 Windows 上,这可能位于 C:\Users\YOURNAME.AndroidStudio1.4\system\caches 之类的路径中。
  3. 重启AS

回答by mahmoud zaher

None of the things mentioned earlier here did actually work for me But then I found this menu entry in the file menu Invalidate Caches/Restartwhich appears to have fixed the problem.

前面提到的所有内容都没有真正对我有用,但后来我在文件菜单Invalidate Caches/Restart 中找到了这个菜单项,它似乎解决了这个问题。

I don't really know what happened in the background but when Android Studio started up again the status bar said Indexing... for a minute or so which apparently did wonders.

我真的不知道后台发生了什么,但是当 Android Studio 再次启动时,状态栏显示 Indexing... 一分钟左右,这显然是奇迹。

if it still not working solution is very simple i have tried also

如果它仍然不起作用 解决方案很简单我也试过

it was just go to helpmenu and check for updatethen restartandroid studio

它只是转到帮助菜单并检查更新然后重新启动android studio

as attached photo

如附件照片

enter image description here

在此处输入图片说明

回答by Pankaj Lilan

Faced the same issue and struggled half day to resolve it.

遇到同样的问题,折腾了半天才解决。

Finally found the solution. If you're facing the situation where the project is building successfully, but Cannot resolve ThirdParty.classerror is coming. Than do following steps:

终于找到了解决办法。如果您遇到项目构建成功,但Cannot resolve ThirdParty.class错误来了的情况。比做以下步骤:

  1. Delete .ideafolder from your project.
  2. Close Android Studio
  3. Restart Android Studio and Importyour project.
  1. 从您的项目中删除.idea文件夹。
  2. 关闭 Android Studio
  3. 重新启动 Android Studio 并导入您的项目。

And you're good to go.

你很高兴去。

回答by tliebeck

I'm running into this quite a bit while in the process of moving code (and libraries) from one module of a project to another. Project compiles/runs, signed APK builds, but AndroidStudio editor insists it can't find classes.

在将代码(和库)从项目的一个模块移动到另一个模块的过程中,我经常遇到这个问题。项目编译/运行,签名的 APK 构建,但 AndroidStudio 编辑器坚持它找不到类。

In my particular scenario, the most effective solution I've found is to modify build.gradle within AndroidStudio such that the editor indicates "Gradle files have changed, a Project Sync may be necessary." and then clicking the "Sync Now" button. In my case, I've not actually had any reason to change the build.gradle file, so I tend to just make an arbitrary change to the file and then reverse it.

在我的特定场景中,我发现的最有效的解决方案是在 AndroidStudio 中修改 build.gradle,以便编辑器指示“Gradle 文件已更改,可能需要项目同步”。然后单击“立即同步”按钮。就我而言,我实际上没有任何理由更改 build.gradle 文件,因此我倾向于对该文件进行任意更改,然后将其反转。

回答by Abhilasha Kudesia

i faced the same problem and i just followed the following steps- take the backup of your android project and delete it from your android studio the again import it it will start working

我遇到了同样的问题,我只是按照以下步骤操作 - 备份您的 android 项目并将其从您的 android studio 中删除,再次导入它将开始工作

回答by Bedant

Happens to me few times. I updated the android studio to the latest version and it solved my problem. This is not a proper solution but it worked in my case. If this does not work try to re install android studio.

发生在我身上几次。我将 android studio 更新到最新版本,它解决了我的问题。这不是一个合适的解决方案,但它在我的情况下有效。如果这不起作用,请尝试重新安装 android studio。

回答by agungandika

Adding answer from Ruchir, all the new things is good but not really good for some.

添加来自 Ruchir 的回答,所有新事物都很好,但对某些人来说并不是很好。

I tried to re-create my environtment and found that newest AS have problem with newest SDK. So All I did was:

我尝试重新创建我的环境,发现最新的 AS 有最新的 SDK 问题。所以我所做的就是:

  • use AS 1.4.1 (stable)
  • downgrade SDK to use v23.0.1.
  • 使用 AS 1.4.1(稳定)
  • 降级 SDK 以使用 v23.0.1。

gradle.build changes as follows:

gradle.build 更改如下:

compile 'com.android.support:appcompat-v7:23.0.1'
compile 'com.android.support:cardview-v7:23.0.1'
compile 'com.android.support:design:23.0.1'
compile 'com.android.support:recyclerview-v7:23.0.1'
compile 'com.android.support:support-v4:23.0.1'

Then do the basics "clean up":

然后做基本的“清理”:

  • Clean project
  • Rebuild Project
  • 清洁工程
  • 重建工程

Done. It's not your code, it's IDE. Consider submit error report to Google about this error.

完毕。这不是您的代码,而是 IDE。考虑向 Google 提交有关此错误的错误报告。

回答by Andre Classen

I got a similar Problem after upgrading my PC and reinstalling Windows. The only solution that worked for me was using jdk 7 instead of 8. Never run into this problem with my old machine and jdk 8.

升级我的电脑并重新安装 Windows 后,我遇到了类似的问题。唯一对我有用的解决方案是使用 jdk 7 而不是 8。我的旧机器和 jdk 8 永远不会遇到这个问题。

回答by Sangharatna

Finally i resolved that issue by just changing JDK..

最后我通过更改 JDK 解决了这个问题。

Firstly right click on android project then go to Module settings.

首先右键单击android项目然后转到模块设置。

Select SDK Location

选择 SDK 位置

Uncheck Use Embedded JDK and browse to install jdk folder.

取消选中使用嵌入式 JDK 并浏览以安装 jdk 文件夹。

done! now rebuild your project and all your java error will gone.

完毕!现在重建你的项目,你所有的 java 错误都会消失。

that happen because of embedded jdk..

发生这种情况是因为嵌入式 jdk ..