【问题标题】:R cannot be resolved to a variable - AndroidR 无法解析为变量 - Android
【发布时间】:2014-03-03 15:47:23
【问题描述】:

我正在学习 Android 开发,我正在使用 Reto Meier 编写的 Professional Android 4 Application Development 一书。我正在使用带有 ADT 插件的 Eclipse 来遵循书中的示例。我已经阅读了几篇关于这个问题的现有帖子,并尝试了几种方法来解决它。我的代码与书中的代码完全相同。请帮助我,我很想继续阅读本书,但我不想跳过过去的事情,但我已经在这个愚蠢的错误上待了三个多小时。这是一个简单的待办事项列表应用程序。这是我的主要活动课程。

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

第 19 行 setContentView(R.layout.activity_to_do_list);

    // Get references to UI widgets

第 22 行 ListView myListView = (ListView)findViewById(R.id.myListView);

第 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文件。然后从 Eclipse 菜单清理并重建您的项目。
  • 试试 Ctrl + Shift + O。它的字母“O”而不是零。

标签: java android eclipse


【解决方案1】:

同样的事情发生在我身上,我花了一天时间来解决这个问题。真的这是一个愚蠢的错误。我只是剪一下我的经验,可能对你有帮助。当你输入这个setContentView(R.layout.activity_to_do_list);时先仔细看看R.l你的起点然后eclipse应该为你打开一个建议看起来像这样

您导入第二个不是 layout-android.R 。第二个是在您的项目中创建的,听到 com.example.gridtest 是项目包名称。然后将您导入的部分集中在您的代码中

好的,看看这个import com.example.gridtest.R;它的重要。如果您已导入此 android.R,则将其删除。谢谢希望它有效。 (你不需要总是这样做,但如果你遇到这种问题,那就这样做)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多