Android 教程错误:R 无法解析为变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11255256/
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
Android tutorial error: R cannot be resolved to a variable
提问by SwimBikeRun
http://developer.android.com/training/basics/firstapp/building-ui.html
http://developer.android.com/training/basics/firstapp/building-ui.html
I have been following this tutorial, but I have two errors, both "R cannot be resolved to a variable". I have made android apps (easy ones) in the past, and I remember this problem being fixed by checking whether or not I have an import R statement (which I don't) and whether or not the project has been cleaned before being built again (I cleaned it and I still get the error). I am at a loss as to what to do. Thanks!
我一直在关注本教程,但我有两个错误,“R 无法解析为变量”。我过去曾制作过 android 应用程序(简单的应用程序),我记得这个问题是通过检查我是否有 import R 语句(我没有)以及项目在构建之前是否已被清理来解决的再次(我清理了它,但仍然出现错误)。我不知所措。谢谢!
*I also want to mention I did see the thread of the same title with 170 hits, and the solution of "delete the import R statement" does not apply to my problem (I don't think) Thanks again
*我还想说我确实看到了170次点击的同名帖子,“删除导入R语句”的解决方案不适用于我的问题(我不认为)再次感谢
All code is straight from the link above, but here it is for convenience
所有代码都直接来自上面的链接,但为了方便起见
activity_my_first.xml
activity_my_first.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<EditText android:id="@+id/edit_message"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="@string/edit_message" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_send" />
</LinearLayout>
MyFirstActivity.java
MyFirstActivity.java
package com.example.myfirstapp;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v4.app.NavUtils;
public class MyFirstActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_first);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_my_first, menu);
return true;
}
}
strings.xml
字符串.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">My First App</string>
<string name="edit_message">Enter a message</string>
<string name="button_send">Send</string>
<string name="menu_settings">Menu Settings</string>
</resources>
edit: When I clean the project, I get this message in the Console:
编辑:当我清理项目时,我在控制台中收到此消息:
[2012-06-29 11:12:38 - MyFirstApp] W/ResourceType( 6140): Bad XML block: header size 91 or total size 0 is larger than data size 0 [2012-06-29 11:12:38 - MyFirstApp] C:\Users\zhong\workspace\eclipse\MyFirstApp\res\menu\activity_my_first.xml:2: error: Error: No resource found that matches the given name (at 'title' with value '@string/menu_settings').
[2012-06-29 11:12:38 - MyFirstApp] W/ResourceType(6140):错误的 XML 块:标头大小 91 或总大小 0 大于数据大小 0 [2012-06-29 11:12:38 - MyFirstApp] C:\Users\zhong\workspace\eclipse\MyFirstApp\res\menu\activity_my_first.xml:2:错误:错误:找不到与给定名称匹配的资源(在“标题”处,值为“@string/menu_settings” )。
edit: added a line to layout xml file <string name="menu_settings">Menu Settings</string>
Fixed cleaning project errors, but I still can't run the project from the two R errors.
编辑:在布局xml文件中添加了一行<string name="menu_settings">Menu Settings</string>
修复了清理项目错误,但我仍然无法从两个R错误中运行该项目。
回答by Omri Barel
I'm working my way through the same example, and had the same (or very similar) problem.
我正在通过相同的示例工作,并且遇到了相同(或非常相似)的问题。
Finally I noticed that there was a tiny little red x on the manifest.xml. Sure enough, it was complaining about this:
最后我注意到 manifest.xml 上有一个小小的红色 x。果然,它在抱怨这个:
android:label="@string/title_activity_hello_world" >
So I added:
所以我补充说:
<string name="title_activity_hello_world">Hello World</string>
to strings.xml and now it works.
到strings.xml,现在它可以工作了。
回答by Ph.Duclos
I had the same issue while following the tutorial. Cleaning the project or cleaning the imports did not solve the problem.
我在学习教程时遇到了同样的问题。清理项目或清理导入并没有解决问题。
How the problem was (simply) solved : I quit Eclipse and relaunched it.
问题是如何(简单地)解决的:我退出 Eclipse 并重新启动它。
回答by Russ
2 possible things
2 可能的事情
The package name specified in the Android Manifest isn't the same as the in the Java files.
Android Manifest 中指定的包名与 Java 文件中的不同。
Or, your IDE hasn't generated the R.java file in the gen/ folder. Try building again (despite the error) and it will usually clear it up.
或者,您的 IDE 尚未在 gen/ 文件夹中生成 R.java 文件。再次尝试构建(尽管有错误),它通常会清除它。
Basically the R.java file is generated for you and in the same package so you can refer to it as simply R.
基本上 R.java 文件是为您生成的,并且在同一个包中,因此您可以将其简称为 R。
回答by Frank
I had android:text="@string/button_send"
Which gave the error: No resource found that matches the given name (at 'text' with value '@string/button_send').
我有android:text="@string/button_send"
哪个给出了错误:No resource found that matches the given name (at 'text' with value '@string/button_send').
I tried to solve the error using the tuturial. But the tutorial fails to tell me to write all the lines required one by one. So I miss to write "<string name="button_send">Send</string>"
in the strings.xml.
我尝试使用tuturial解决错误。但是教程没有告诉我把所有需要的行一一写出来。所以我错过了"<string name="button_send">Send</string>"
在strings.xml 中的写法。
After adding the previous line the error disappear! Which I believe is something similar to the original problem in this thread.
添加上一行后,错误消失!我认为这与该线程中的原始问题类似。
So if I had copied the whole lines from the given tutorial it may had not happened, but by typing our-self is how I think is the best way of learning.
因此,如果我从给定的教程中复制了整行代码,它可能不会发生,但是通过输入 our-self 是我认为最好的学习方式。
回答by yarin
I found the solution:
我找到了解决方案:
go to your project->res->menu
than open the XML
file there and delete this line:
去你那里project->res->menu
打开XML
文件并删除这一行:
android:title="@string/menu_settings"
I deleted it and it start to work,after 3 hours of the same warning in the problem section.
在问题部分出现相同警告 3 小时后,我将其删除并开始工作。
It even make sense when you read it, "at title
with value @string/menu_settings
"
当你阅读它时,它甚至是有道理的,“title
有价值@string/menu_settings
”
回答by please delete me
Right click on the Project, select "Android Tools" > "Add Support Library" > "Android Support Library, revision 11" > "Install"
右键单击项目,选择“Android 工具”>“添加支持库”>“Android 支持库,修订版 11”>“安装”
回答by Leigh
In my case, the problem was that I was on 64-bit Linux, and the required 32-bit libraries were not installed, meaning that the build tools could not generate R.java.
就我而言,问题是我使用的是 64 位 Linux,并且没有安装所需的 32 位库,这意味着构建工具无法生成 R.java。
Running this command, then cleaning the project, fixed the problem:
运行此命令,然后清理项目,解决了问题:
sudo apt-get install ia32-libs
回答by Sven Schirmer
Make sure the executables (aapt etc.) in sdk/build-tools/android-xyz/* are executable if you are running the default download on your Linux box...
如果您在 Linux 机器上运行默认下载,请确保 sdk/build-tools/android-xyz/* 中的可执行文件(aapt 等)是可执行的...
回答by Will
I had the same error trying to do the first tutorial. To get the app to run I edited MainActivity.java so it looked like this
我在尝试做第一个教程时遇到了同样的错误。为了让应用程序运行我编辑了 MainActivity.java 所以它看起来像这样
package com.example.myfirstapp;
import android.app.Activity; import android.os.Bundle;
导入 android.app.Activity; 导入 android.os.Bundle;
public class MainActivity extends Activity {
公共类 MainActivity 扩展 Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
}
回答by Dheeresh Singh
Pleas confirm there is no error in
请确认没有错误
1- Project folder
2- Problem View of eclipse
3- Console
4- error log as in image
1- 项目文件夹 2- eclipse 的问题视图 3- 控制台 4- 错误日志如图