【问题标题】:R Errors In Eclipse [duplicate]Eclipse中的R错误[重复]
【发布时间】:2014-01-20 05:29:43
【问题描述】:
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

【问题讨论】:

  • 您是否创建了 XML 文件?
  • 删除import android.R;
  • 如果您在 Eclipse 中工作,请转到底部附近的“问题”选项卡并打开它。还可以在左侧的包资源管理器中查看您的资源。很有可能,您会发现您的资源存在构建问题(格式错误的 xml 文件可以解决此问题)。您的资源的任何问题都将导致“R”无法构建或出现问题。简而言之,看起来 res/layout/main 丢失或无法编译,并且“id”和“delete” id 也丢失了。

标签: java android eclipse r.java-file


【解决方案1】:

删除对android.R的导入

被命名空间弄糊涂了

【讨论】:

    【解决方案2】:

    您必须删除
    import android.R

    然后清理并重建项目。 将生成 R。

    【讨论】:

      【解决方案3】:
      import android.R; 
      

      改为使用

      import yourpackgename.R;
      

      【讨论】:

        【解决方案4】:

        先删除导入

        import android.R;
        

        然后去排队

        setContentView(R.layout.main); 
        

        将其悬停并添加主包的导入,然后导入 android.R;

        【讨论】:

          猜你喜欢
          • 2014-04-04
          • 2014-04-15
          • 2013-05-14
          • 2014-04-30
          • 1970-01-01
          • 1970-01-01
          • 2012-11-16
          • 1970-01-01
          相关资源
          最近更新 更多