Eclipse中的R错误[重复](R Errors In Eclipse [duplicate])

系统教程 行业动态 更新时间:2024-06-14 16:57:18
Eclipse中的R错误[重复](R Errors In Eclipse [duplicate])

这个问题在这里已经有了答案:

R无法解析 - Android错误 106答案 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

This question already has an answer here:

R cannot be resolved - Android error 106 answers 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(); } }

This is my code above.

The three errors that I am receiving are:

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

最满意答案

你必须删除 import android.R

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

You have to remove import android.R

Then clean and rebuild project. R will be generated.

更多推荐

本文发布于:2023-04-12 21:00:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/b971da60213227992e3910351cc3ee7a.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:错误   Eclipse   duplicate   Errors

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!