问题描述
http://developer.android.com/training/basics/firstapp/building-ui.html
我一直在学习本教程,但是有两个错误,两个都"R无法解析为变量".我过去曾经制作过android应用程序(简单的应用程序),并且我记得通过检查是否有import R语句(我没有)以及在构建项目之前是否已将其清理来解决此问题.再次(我清理了它,但仍然收到错误).我不知所措.谢谢!
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!
*我还要提及的是,我确实看到了具有170个匹配项的相同标题的线程,并且删除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
所有代码都直接来自上面的链接,但这是为了方便
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
strings.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:
在布局xml文件<string name="menu_settings">Menu Settings</string>
中添加了一行修复了清理项目错误,但仍然无法从两个R错误运行项目.
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.
推荐答案
我正在研究同一示例,并且遇到了相同(或非常相似)的问题.
I'm working my way through the same example, and had the same (or very similar) problem.
最后,我注意到manifest.xml上有一个小的红色x.果然,它在抱怨:
Finally I noticed that there was a tiny little red x on the manifest.xml.Sure enough, it was complaining about this:
android:label="@string/title_activity_hello_world" >
所以我添加了:
<string name="title_activity_hello_world">Hello World</string>
到strings.xml,现在可以使用了.
to strings.xml and now it works.
这篇关于Android教程错误:R无法解析为变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!