Android Studio“无法解析符号字符串”

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

Android Studio "cannot resolve symbol String"

androidandroid-studio

提问by Christofer Ohlsson

I have a small project that was started in Eclipse. I then exported it to a gradle file, and imported it to AS (0.5.7).

我有一个在 Eclipse 中启动的小项目。然后我将它导出到一个 gradle 文件,并将它导入到 AS (0.5.7)。

At first, not much seemed to work as it should, but after a "build => make project", I didn't seem to get any highlighted errors or so.

起初,似乎并没有像它应该的那样工作,但是在“构建 => 制作项目”之后,我似乎没有得到任何突出显示的错误左右。

So I tried to run the app to an emulated device. Well, the device never launched, and now I get red squiggly lines under mentions of "String", "ArrayList" etc, saying it "cannot resolve the symbol".

所以我尝试将应用程序运行到模拟设备上。好吧,该设备从未启动,现在我在提到“String”、“ArrayList”等时看到红色波浪线,说它“无法解析符号”。

What the f?

什么?

I have tried cleaning and rebuilding, as well as "sync project with gradle files".

我尝试过清理和重建,以及“将项目与 gradle 文件同步”。

Where do I go from here? I want to get going with developing in AS so bad!

我从这里去哪里?我想开始在 AS 中开发,太糟糕了!

edit: Screenshot of project setup: http://i.imgur.com/ycNyPaT.png

编辑:项目设置截图:http: //i.imgur.com/ycNyPaT.png

Contents of build.gradle:

build.gradle 的内容:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.9.+'
    }
}
apply plugin: 'android'

dependencies {
    compile fileTree(dir: 'libs', include: '*.jar')
}

android {
    compileSdkVersion 19
    buildToolsVersion "19.0.1"

    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']
            resources.srcDirs = ['src']
            aidl.srcDirs = ['src']
            renderscript.srcDirs = ['src']
            res.srcDirs = ['res']
            assets.srcDirs = ['assets']
        }

        // Move the tests to tests/java, tests/res, etc...
        instrumentTest.setRoot('tests')

        // Move the build types to build-types/<type>
        // For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ...
        // This moves them out of them default location under src/<type>/... which would
        // conflict with src/ being used by the main source set.
        // Adding new build types or product flavors should be accompanied
        // by a similar customization.
        debug.setRoot('build-types/debug')
        release.setRoot('build-types/release')
    }
}

回答by SILINIK

There is simpler and I think more correct way:

有更简单的,我认为更正确的方法:

Just select menu item 'File/Invalidate Caches/Restart...'

只需选择菜单项“文件/使缓存无效/重新启动...”

For me this successfully resolved the issue (was caused by surprising power off of PC)

对我来说,这成功解决了问题(由 PC 意外断电引起)

enter image description here

在此处输入图片说明

回答by Jakub Szczygie?

So project arrangement should be as follows:

所以项目安排应该如下:

  • create app folder within your project.
  • within app folder make following folders: libs and src
  • inside src create main folder
  • inside main create java and assets
  • move contents of old src to java
  • move contents of old libs to libs
  • move res folder to src
  • move AndroidManifest.xml to src
  • move assets folder into src
  • create build.gradle inside app folder with following content:
  • 在您的项目中创建 app 文件夹。
  • 在 app 文件夹中创建以下文件夹:libs 和 src
  • 在src里面创建主文件夹
  • 在 main 里面创建 java 和 assets
  • 将旧 src 的内容移动到 java
  • 将旧库的内容移动到库
  • 将 res 文件夹移动到 src
  • 将 AndroidManifest.xml 移动到 src
  • 将资产文件夹移动到 src
  • 使用以下内容在 app 文件夹中创建 build.gradle:
apply plugin: 'android'
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
}
android {
compileSdkVersion 19
buildToolsVersion "19.0.1"
}
apply plugin: 'android'
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
}
android {
compileSdkVersion 19
buildToolsVersion "19.0.1"
}
  • create settings.gradle in project root with following content:
  • 在项目根目录中创建 settings.gradle ,内容如下:

include 'app'

包括“应用程序”

  • build.gradle in root should have following structure:
  • root 中的 build.gradle 应具有以下结构:

buildscript { repositories { mavenCentral() } dependencies { classpath 'com.android.tools.build:gradle:0.9.+' } }

buildscript { 存储库 { mavenCentral() } 依赖项 { classpath 'com.android.tools.build:gradle:0.9.+' } }

回答by Thommy

Just writing this for people like me, who also landed here through googling this error. What actually solved it for current Android Studio 3.1.+was this

只是为像我这样的人写这篇文章,他们也是通过谷歌搜索这个错误来到这里的。目前实际解决的Android Studio 3.1.+这个

回答by TanzeemAlam

You just need to sync the project as you have opened an external project from a zip file in the android studio. After you sync the project, you will see folders like Java,res folder,etc, instead of these not useful folders. And also, all the errors will be gone too.

您只需要同步项目,因为您已经从 android studio 中的 zip 文件打开了一个外部项目。同步项目后,您将看到 Java、res 文件夹等文件夹,而不是这些没有用的文件夹。而且,所有错误也将消失。

When you open the external project, it will show like this. https://photos.app.goo.gl/BFa113X9DWH8eNDt7

当您打开外部项目时,它将显示如下。 https://photos.app.goo.gl/BFa113X9DWH8eNDt7

Then you click on the install, it will install the required components required for the project and then it will sync the project. After syncing, the Gradle will be build and your project is ready to run.

然后你点击安装,它会安装项目所需的组件,然后它会同步项目。同步后,Gradle 将被构建并且您的项目已准备好运行。

回答by Ken Ratanachai S.

Remove the following line from the file that reported with the error if there is.

如果有错误,请从报告错误的文件中删除以下行。

import static com.google.android.gms.internal.a.R;