【发布时间】: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