java - Eclipse 中的 R 错误

标签 java android eclipse r.java-file

<分区>

package de.vogella.android.sqlite.first;

import java.util.List;
import java.util.Random;

import android.R;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;

public class TestDatabaseActivity extends ListActivity {
  private CommentsDataSource datasource;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    datasource = new CommentsDataSource(this);
    datasource.open();

    List<Comment> values = datasource.getAllComments();

    // use the SimpleCursorAdapter to show the
    // elements in a ListView
    ArrayAdapter<Comment> adapter = new ArrayAdapter<Comment>(this,
        android.R.layout.simple_list_item_1, values);
    setListAdapter(adapter);
  }

  // Will be called via the onClick attribute
  // of the buttons in main.xml
  public void onClick(View view) {
    @SuppressWarnings("unchecked")
    ArrayAdapter<Comment> adapter = (ArrayAdapter<Comment>) getListAdapter();
    Comment comment = null;
    switch (view.getId()) {
    case R.id.add:
      String[] comments = new String[] { "Cool", "Very nice", "Hate it" };
      int nextInt = new Random().nextInt(3);
      // save the new comment to the database
      comment = datasource.createComment(comments[nextInt]);
      adapter.add(comment);
      break;
    case R.id.delete:
      if (getListAdapter().getCount() > 0) {
        comment = (Comment) getListAdapter().getItem(0);
        datasource.deleteComment(comment);
        adapter.remove(comment);
      }
      break;
    }
    adapter.notifyDataSetChanged();
  }

  @Override
  protected void onResume() {
    datasource.open();
    super.onResume();
  }

  @Override
  protected void onPause() {
    datasource.close();
    super.onPause();
  }

} 

这是我上面的代码。

我收到的三个错误是:

setContentView(R.layout.main); 
                         ^
                      main cannot be resolved or is not a field

&&&

case R.id.add:
           ^
        add cannot be resolved or is not a field

&&&

case R.id.delete:
           ^
        delete cannot be resolved or is not a field

最佳答案

移除对 android.R 的导入

它被命名空间弄糊涂了

关于java - Eclipse 中的 R 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20888505/

相关文章:

java - Android 超时监听器

java - Eclipse 条件断点损坏了吗?

java - JSP 中自动省略空格

android - 删除所有本地日历事件有效,但会触发删除太多事件的错误

php - Ubuntu - Eclipse 和 Netbeans 的替代品

java - 尝试使用 2 个不同的按钮打开 2 个不同的 xml 页面

java - 使用 Java Optional 寻找更实用的方法?

android - 找不到 Kotlin 改造上传图片

Android:如何:显示带有移动当前位置指针的 map (静止图像文件)

Eclipse 插件无法在 Ganymede 中工作,但在 Galileo 中工作