java 无法解析符号 R
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26573456/
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
Cannot Resolve symbol R
提问by SparkyM
I was working on Android Studio for adding a short toast message.(I was making an Android Wear Application)
我正在 Android Studio 上添加简短的 toast 消息。(我正在制作一个 Android Wear 应用程序)
I couldn't know why this code has error on 'symbol R' . It says "Cannot Resolve Symbol R."
我不知道为什么这段代码在 'symbol R' 上有错误。它说“无法解析符号 R”。
import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.wearable.view.WatchViewStub;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MyActivity extends Activity {
private TextView mTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
//getnextpage
Button getnextpage;
getnextpage = (Button) findViewById(R.id.getnextpage);
getnextpage.setOnClickListener(new OnclickListener() {
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Here is 2nd Page", Toast.LENGTH_LONG).show();
}
});
final WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub);
stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {
@Override
public void onLayoutInflated(WatchViewStub stub) {
mTextView = (TextView) stub.findViewById(R.id.text);
}
});
}
}
回答by
Below some procedures are mentioned :
下面提到了一些程序:
1) Try Build
-> Clean/Rebuild
project .
1)尝试Build
- >Clean/Rebuild
项目。
2) Manually typing import <package name>.R
, If in import R is missing.
2) 手动输入import <package name>.R
,如果在导入 R 中丢失。
3) complete all resources of its corresponding XML
file .
3) 完成其对应XML
文件的所有资源。
I hope any one of them would be helpful for you as there is no Particular way to resolve this issue.
我希望他们中的任何一个都会对您有所帮助,因为没有解决此问题的特定方法。
回答by JasonYang
If in your case in imports R
is missing you Have to import R
manually.
如果在您的情况下R
缺少导入,则必须R
手动导入。
import <packagename>.R;
Replace <packagename>
with your own package name.
替换<packagename>
为您自己的包名。
In your code, R
was not imported, therefore your Android Studio
might got confused to find resources in your package.
在您的代码中,R
未导入,因此您Android Studio
可能会在包中查找资源时感到困惑。
回答by Rushat Rai
I know others have said most of these points, but this should serve as a sum-up and hopefully bring up some new solutions!
我知道其他人已经说了大部分这些要点,但这应该作为一个总结,并希望提出一些新的解决方案!
If only the letter 'R' is red, it means there is something wrong with your R file (found at build\generated\source\r\debug\com\domain.projectname\R
). Don't go around editing your R file- because each time the project is cleaned and built/rebuilt the R file regenerates (it completely resets without fail). There are a few possible solutions:
如果只有字母“R”是红色的,则表示您的 R 文件(在 中找到build\generated\source\r\debug\com\domain.projectname\R
)有问题。不要四处编辑您的 R 文件 - 因为每次清理和构建/重建项目时,R 文件都会重新生成(它完全重置而不会失败)。有几种可能的解决方案:
1) You might have written import android.R;
at the top of your Activity. You need to have a different R file imported. This means if your domain is "example" and your project name is "project" then your import should be: import com.example.project.R
1) 您可能已经import android.R;
在 Activity 的顶部写过。您需要导入不同的 R 文件。这意味着如果您的域是“example”并且您的项目名称是“project”,那么您的导入应该是:import com.example.project.R
2) R is a build file. That means it disappears when you clean and generates anew when you build, as previously said above. Make sure you build or rebuild your project after cleaning it.
2) R 是一个构建文件。这意味着它在您清理时消失并在您构建时重新生成,如上所述。确保在清理后构建或重建项目。
3) Maybe your gradle project sync has not completed. Wait until all processes have finished running!
3) 可能你的 gradle 项目同步还没有完成。等待所有进程运行完毕!
4) It is also possible your R file is corrupt or missing. If the above don't work, you can come to this conclusion. It has a very easy fix, because as I explained earlier when you rebuild a new R file is generated. So just navigate to Build
and click Clean project
, then when that's finished, click Rebuild project
. With the new R file your problems should be gone.
4) 您的 R 文件也可能已损坏或丢失。如果以上都不行,你可以得出这个结论。它有一个非常简单的修复方法,因为正如我之前解释的那样,当您重建一个新的 R 文件时会生成。因此,只需导航到Build
并单击Clean project
,然后在完成后单击Rebuild project
。使用新的 R 文件,您的问题应该消失了。
P.S: It's nearly impossible to give an accurate answer without all relevant code provided
PS:如果没有提供所有相关代码,几乎不可能给出准确答案
回答by Crime_Master_GoGo
It happens if your class is missing the link of R.Class
which you may have to import.
So, Before starting the class you can simply start by
如果您的课程缺少R.Class
您可能必须导入的链接,就会发生这种情况。所以,在开始上课之前,你可以简单地开始
import Your_Package_name.R;
import Your_Package_name.R;
E.g.
例如
import com.example.myproject.R;
import com.example.myproject.R;
You can manually write this when Clean/Rebuild
project is not the solution.
当Clean/Rebuild
项目不是解决方案时,您可以手动编写它。
Use-case:
用例:
import javax.crypto.spec.SecretKeySpec;
import javax.net.ssl.HttpsURLConnection;
import com.example.jsonwebservice.R;
This worked for me.
这对我有用。
回答by grooble
Just in case any of the above answers don't work, the following solved the issue in my case...
以防万一上述任何答案不起作用,以下解决了我的问题......
Here's what I tried:
这是我尝试过的:
Multiple "cleans", "rebuilds" and "invalidate caches restart"
Attempts to manually add [my package].R,
多次“清理”、“重建”和“无效缓存重启”
尝试手动添加 [我的包].R,
This didn't work, so I uninstalled Android Studio, and reinstalled the latest version.
这不起作用,所以我卸载了 Android Studio,并重新安装了最新版本。
The accepted answer to the above link has the following steps (which I reproduce here):
上述链接的公认答案具有以下步骤(我在此处复制):
- Uninstall from installer. (via control panel for windows)
Remove Android Studio files
Go to your user folder (%USERPROFILE%), and delete .android, .AndroidStudio and any analogous directories with versions on the end, i.e. .AndroidStudio1.2, as well as .gradle and .m2 if they exist. Then go to %APPDATA% and delete the JetBrains directory. Finally, go to C:\Program Files and delete the Android directory.
Remove the SDK
To delete any remains of the SDK, go to %LOCALAPPDATA% and delete the Android directory.
Delete Android Studio Projects
Android Studio creates projects in a folder %USERPROFILE%\AndroidStudioProjects, which you may want to delete.
- 从安装程序卸载。(通过 Windows 的控制面板)
删除 Android Studio 文件
转到您的用户文件夹 (%USERPROFILE%),然后删除 .android、.AndroidStudio 和任何以版本结尾的类似目录,即 .AndroidStudio1.2,以及 .gradle 和 .m2(如果存在)。然后转到 %APPDATA% 并删除 JetBrains 目录。最后,转到 C:\Program Files 并删除 Android 目录。
删除 SDK
要删除 SDK 的任何剩余部分,请转到 %LOCALAPPDATA% 并删除 Android 目录。
删除 Android Studio 项目
Android Studio 在文件夹 %USERPROFILE%\AndroidStudioProjects 中创建项目,您可能希望删除该文件夹。
I skipped 4), then uninstalled a separate Java 7 installation I had, for good measure.
我跳过了 4),然后卸载了一个单独的 Java 7 安装,这是很好的衡量标准。
Next I downloaded the latest Android Studio (3.3.2) and installed it.
接下来我下载了最新的 Android Studio (3.3.2) 并安装了它。
A bit drastic perhaps, but now everything is working and the "Cannot resolve symbol R" error is gone.
也许有点激烈,但现在一切正常,“无法解析符号 R”错误消失了。
回答by alfasin
The thing that finally did the trick for me was looking at the "event log" tab in Android Studio where I saw:
最终对我有用的事情是查看 Android Studio 中的“事件日志”选项卡,在那里我看到:
Frameworks detected: Android framework is detected in the project Configure
Frameworks detection:在项目Configure中检测到Android框架
clicking on "Configure" set the AndroidManifest.xml as configuration-file of the project (identified it as "android") and only then all the "R" errors went away and I was able to compile & run.
单击“配置”将 AndroidManifest.xml 设置为项目的配置文件(将其标识为“android”),然后所有“R”错误都消失了,我才能编译和运行。
Before doing that - all the actions I've tried such as: "clean", "rebuild" and restarting Android studios - didn't work.
在这样做之前 - 我尝试过的所有操作,例如:“清理”、“重建”和重新启动 Android 工作室 - 都不起作用。
回答by Karue Benson Karue
It seems like Gradle is taking too long time to execute for your computer. Wait for the Gradle to finish building the project.
似乎 Gradle 为您的计算机执行所需的时间太长。等待 Gradle 完成项目的构建。
回答by Kudehinbu Oluwaponle
I resolved it by editing my
build.gradle
file.
我通过编辑我的build.gradle
文件解决了它
。
The compiledSdkVersion
must align with your appcompat
dependency.
在compiledSdkVersion
必须使用一致appcompat
的依赖。
My compileSdkVersion
was 22 so I edited my appcompat
dependency to
我compileSdkVersion
当时 22 岁,所以我将appcompat
依赖项编辑为
compile 'com.android.support:appcompat-v7:22.2.1'
So, I hope this can be a solution.
所以,我希望这可以成为一个解决方案。