Java Android Studio 2.1 无法解析符号“R”

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

Android Studio 2.1 Cannot resolve symbol 'R'

javaandroidandroid-studior.java-file

提问by Infinite Loops

Android Studio 2.1, Java V8 Update 91, Gradle 2.1.0 .

Android Studio 2.1、Java V8 更新 91、Gradle 2.1.0。

I know that this questions has been asked many times here, but none of the recommendation solutions works out.

我知道这里已经多次问过这个问题,但没有一个推荐解决方案有效。

In a sudden after compiling ready to run on Emulator, my IDE give an error at the R.id attribute. Every Java files suddenly give the same error even I don't make any changes to the file.

在编译准备好在模拟器上运行后,我的 IDE 突然在 R.id 属性处给出错误。即使我没有对文件进行任何更改,每个 Java 文件也会突然出现相同的错误。

I tried to make a new project, see if things different but the error keep coming. In my mind it could be internal error, again. Even I just update my IDE just now.

我尝试创建一个新项目,看看情况是否有所不同,但错误不断出现。在我看来,这可能又是内部错误。即使我刚刚更新了我的 IDE。

The Java file;

Java文件;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_science);

    list_View = (ListView) findViewById(R.id.maintable); 
    String[] Days = new String[] { "Sunday", "Monday", "Tuesday", "Wednesday","Thursday", "Friday", "Saturday"};
    dayList.addAll(Arrays.asList(Days));
    adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, dayList);
    list_View.setAdapter(adapter);

    NfcAdapter nfc = NfcAdapter.getDefaultAdapter(this);

    if (nfc.isEnabled()) {
        adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, dayList);
        Toast.makeText(this, "NFC turned ON", Toast.LENGTH_SHORT).show();
    }

    else {
        Toast.makeText(this, "Please turn On NFC", Toast.LENGTH_SHORT).show();
    }

}

The Ris the error.

R是错误。

XML file that associate to the Java;

与 Java 关联的 XML 文件;

<ListView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/maintable"
    >
</ListView>

Is this, again, an internal error?

这又是一个内部错误吗?

采纳答案by Infinite Loops

I try to change the Gradle version from 2.1.0 to 2.0.0, even in AS ver 2.1, ignoring the Instant Run performance and its work out. The little R error is now gone. Now, I tried to change back to 2.1.0, the R error also gone..!! Sometime this little thing drive me crazy, a bit.

我尝试将 Gradle 版本从 2.1.0 更改为 2.0.0,即使在 AS 版本 2.1 中,也忽略了 Instant Run 性能及其效果。小 R 错误现在消失了。现在,我尝试改回 2.1.0,R 错误也消失了..!!有时这件小事让我有点发疯。

Shame on me.

为我感到羞耻。

回答by Hadas Kaminsky

Try syncing the project, go to Tools > Android > Sync Project with Gradle Files

尝试同步项目,转到 Tools > Android > Sync Project with Gradle Files

UPDATE

更新

If you're using Android Studio 3.3, use the Sync Project with Gradle Filesicon

如果您使用的是 Android Studio 3.3,请使用Sync Project with Gradle Files图标

enter image description here

在此处输入图片说明

回答by Munaz

Try performing a clean and rebuild by going to Build > Clean Projectand Build > Make Project

尝试通过转到Build > Clean Project和执行清理和重建Build > Make Project

回答by Zain ul abdeen

If you are using google play services and your compilesdk version is lower than 23 then download the new sdk and update your compile version. After that try cleaning and rebuilding. It will be fixed

如果您使用的是 google play 服务并且您的 compilesdk 版本低于 23,请下载新的 sdk 并更新您的编译版本。之后尝试清洁和重建。它会被修复

回答by Gladys Sanchez

Maybe the error may cause for the tools, trying install Android SDK Build Tools from Tools > Android > SDK Manager. or maybe this error can be caused by adding an activity to a namespace that is different to the root, you can trying >>Import com.example.MyApplication.R; or your also can clean o rebuild your project in Build > Clean Project or Build > Rebuild Project

也许错误可能会导致工具,尝试从工具 > Android > SDK 管理器安装 Android SDK 构建工具。或者这个错误可能是由于将活动添加到与根不同的命名空间引起的,您可以尝试 >>Import com.example.MyApplication.R; 或者您也可以在 Build > Clean Project 或 Build > Rebuild Project 中清理或重建您的项目

回答by nishul tomar

Have you tried Rebuild Project go to Build> Rebuild Projectand perform it without doing the Clean project I had the same problem & it worked for me

您是否尝试过重建项目,请转到“ 构建”>“重建项目”并在不执行“清理”项目的情况下执行它我遇到了同样的问题并且对我有用

回答by Gustavomcls

Sometimes the R.java class is not created when there is some error into an xml file (like activity_main.xml). For example, if you change some attribute namethat has a reserved word that cannot be changed. Like this: if you changed the attribute called: idto another name like
id_A_Word_With_Error_Herein this code that can be part of the activity_main.xml, your error (R class not created) can occur.

有时,当 xml 文件(如 activity_main.xml)出现错误时,不会创建 R.java 类。例如,如果您更改某些具有无法更改的保留字的属性名称。像这样:如果您在此代码中将名为id的属性更改为另一个名称,例如
id_A_Word_With_Error_Here,它可以是 activity_main.xml 的一部分,则可能会发生错误(未创建 R 类)。

<android.support.v7.widget.RecyclerView
            android:id="@+id/messageRecyclerView"

and there is an error like:

并且有如下错误:

  <android.support.v7.widget.RecyclerView
            android:id_A_Word_With_Error_Here="@+id/messageRecyclerView"

If clean, rebuild did not work and there is no error with import like import com.something..., my suggestion is to search an error into xml files.

如果干净,重建不起作用并且 import like 没有错误 import com.something...,我的建议是在 xml 文件中搜索错误。

回答by Chris S

I have just gone through this nightmare. Ended up removing all xml code to notepad until it was fixed. Then reintroduced sections of code until problem appeared. Thinking back, it all started when I pasted some code using WordPad. Apparently WordPad contains a different coding or something so bet to avoid. Anyway just check your white space in xml by deleting it all to rid yourself of any hidden characters.

我刚刚经历了这个噩梦。最终将所有 xml 代码删除到记事本,直到它被修复。然后重新引入部分代码,直到出现问题。回想起来,这一切都是从我使用写字板粘贴一些代码开始的。显然写字板包含不同的编码或某些东西,所以要避免。无论如何,只需通过删除所有隐藏字符来检查 xml 中的空白区域。

回答by jiar wang

I've seen this problem many times. The right solution is:

我已经多次看到这个问题。正确的解决办法是:

When you type R, the Android Studio will give you many different namespaces for Rto choose from, that look like R(***.***.***), but what you really need is the namespace that ends with ***.myProject. (Note: myProject is your project name), e.g: R(com.example.milka.demoProject).

当您键入 时R,Android Studio 会为您提供许多不同的命名空间供R您选择,看起来像R(***.***.***),但您真正需要的是以 结尾的命名空间***.myProject。(注意:myProject 是您的项目名称),例如:R(com.example.milka.demoProject).

If you choose any other namespace, you will get the error message, so you have to delete the unneeded import, and re-choose your R

如果您选择任何其他命名空间,您将收到错误消息,因此您必须删除不需要的import,然后重新选择您的R