我遵循教程将数据从sqlite数据库填充到Android中的微调器(下拉菜单)中。不过,我得到的错误: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,所以它可能是简单/明显的东西,但任何帮助,非常感谢!
感谢您的帮助,代码的第二部分带来了更多的错误,例如:“不能在静态上下文中使用它”和“无法对非静态字段mDb进行静态引用”。使用第一部分还会产生以下错误:对于类型DatabaseAdapter,未定义fetchAllCategories()方法 – user319940 2011-03-10 21:42:32
必须用您在自己的实现中使用的类名替换DatabaseAdapter。 – 2011-03-10 21:46:41
classname是DatabaseAdapter,它表示构造函数需要另一个参数,所以我刚刚使用了DatabaseAdapter myDbAdapter = new DatabaseAdapter(null);这似乎已经解决了这个问题,但我仍然得到类别游标的错误是:类别游标无法解析 – user319940 2011-03-10 21:50:16