我正在学习Android开发,我正在使用由Reto Meier编写的专业Android 4应用程序开发书。我将Eclipse与ADT插件一起使用,以遵循本书中的示例。我已经阅读了几篇关于这个问题的现有文章,并尝试了几种方法来解决这个问题。我的代码与本书中的代码完全相同。请帮助我,我急于在书中继续前进,但我不想跳过过去的事情,但我已经在这个愚蠢的错误超过三个小时。这是一个简单的待办事项列表应用程序。这是我的主要Activity类。R无法解析为变量 - Android
package com.example.todoolist;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
public class ToDoListActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Inflate your View
LINE 19的setContentView(R.layout.activity_to_do_list);
// Get references to UI widgets
LINE 22的ListView myListView =(ListView中)findViewById(R.id.myListView);
LINE 23 final EditText myEditText =(EditText)findViewById(R.id.myEditText);
// Create the Array List of to do items
final ArrayList<String> todoItems = new ArrayList<String>();
// Create the Array Adapter to bind the array to the List View
final ArrayAdapter<String> aa;
aa = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
todoItems);
// Bind the Array Adapter to the List View
myListView.setAdapter(aa);
myEditText.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN)
if ((keyCode == KeyEvent.KEYCODE_DPAD_CENTER) ||
(keyCode == KeyEvent.KEYCODE_ENTER)) {
todoItems.add(0, myEditText.getText().toString());
aa.notifyDataSetChanged();
myEditText.setText("");
return true;
}
return false;
}
});
}
}
这是我的XML文件...
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/myEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/addItemHint"
android:contentDescription="@string/addItemContentDescription"
/>
<ListView
android:id="@+id/myListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
而我的strings.xml ...
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">ToDoList</string>
<string name="addItemHint">New To Do Item</string>
<string name="addItemContentDescription">New To Do Item</string>
</resources>
我去我的Android SDK管理器已经安装和所有的构建工具没有安装,然后重新启动SDK管理器并多次清理我的项目,并重新启动Eclipse,甚至重新启动我的电脑。请帮助,在这一点上,我只想继续前进,并修复这个愚蠢的错误。我还添加了导入语句,并删除了其他职位人员建议的导入语句。它没有解决这个问题。 Eclipse在Activity类(第19,22和23行)中给出了“R无法解析为变量”错误三次。
Eclipse是否在任何XML资源文件中显示任何错误?这是ADT不会产生“R”并导致此错误的常见原因。如果你的资源很好,我会尝试一个干净的和重建。 –
尝试清理您的项目或刷新它。 –
可能重复的[R无法解析 - Android错误](http://stackoverflow.com/questions/885009/r-cannot-be-resolved-android-error) – Will