Android spinner - 无法静态引用非静态方法帮助
我按照教程将 SQLite 数据库中的数据填充到 Android 中的微调器(下拉列表)中。但是,我收到错误:
Cannot make a static reference to the non-static method fetchAllCategories() from the type DatabaseAdapter
我的代码如下:
在 EditTask 中:
private void fillData() {
Cursor categoriesCursor;
Spinner categoriesSpinner = (Spinner) findViewById(R.id.spinDept);
categoriesCursor = DatabaseAdapter.fetchAllCategories();
startManagingCursor(categoriesCursor);
String[] from = new String[] { DatabaseAdapter.CAT_NAME };
int[] to = new int[] { R.id.tvDBViewRow }; //this part hasnt been implemented in to the layout yet
SimpleCursorAdapter categoriesAdapter = new SimpleCursorAdapter(this,
R.layout.db_view_row, categoriesCursor, from, to);
categoriesSpinner.setAdapter(categoriesAdapter);
}
在我的 DatabaseAdapter 类中,我有以下内容:
public Cursor fetchAllCategories() {
if (mDb == null) {
this.open();
}
String tableName = "CAT_TABLE";
return mDb.query(tableName, new String[] { CAT_ID, CAT_NAME }, null,
null, null, null, null);
}
有问题的代码行是:
categoriesCursor = DatabaseAdapter.fetchAllCategories();
我对 Java/Android 很陌生,所以它可能很简单/显而易见,但非常感谢任何帮助!
I have followed a tutorial to populate data from an sqlite database in to a spinner (dropdown) in Android. However, I am getting the error:
Cannot make a static reference to the non-static method fetchAllCategories() from the type DatabaseAdapter
My code is as follows:
In EditTask:
private void fillData() {
Cursor categoriesCursor;
Spinner categoriesSpinner = (Spinner) findViewById(R.id.spinDept);
categoriesCursor = DatabaseAdapter.fetchAllCategories();
startManagingCursor(categoriesCursor);
String[] from = new String[] { DatabaseAdapter.CAT_NAME };
int[] to = new int[] { R.id.tvDBViewRow }; //this part hasnt been implemented in to the layout yet
SimpleCursorAdapter categoriesAdapter = new SimpleCursorAdapter(this,
R.layout.db_view_row, categoriesCursor, from, to);
categoriesSpinner.setAdapter(categoriesAdapter);
}
And in my DatabaseAdapter class I have the following:
public Cursor fetchAllCategories() {
if (mDb == null) {
this.open();
}
String tableName = "CAT_TABLE";
return mDb.query(tableName, new String[] { CAT_ID, CAT_NAME }, null,
null, null, null, null);
}
The offending line of code is:
categoriesCursor = DatabaseAdapter.fetchAllCategories();
I'm pretty new to Java/Android so it may be something simple/obvious but any help is much appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您必须首先实例化 DatabaseAdapter 对象。
例如:
You have to first instantiate a DatabaseAdapter object.
eg:
为了能够使您的代码正常工作,您需要将方法 fetchAllCategoires() 声明为 static,如下所示:
因为您尚未实例化 DatabaseAdapter 对象,所以您不能仅通过类名引用调用它的方法之一,除非出现关键字 static在方法声明中。
to be able to make your code work, you need to declare the method fetchAllCategoires() as static as follows:
Because you have not instantiated a DatabaseAdapter object, you cannot just call one of it's methods by a class name reference unless the keyword static appears in the method declaration.