我有一个SQLite数据库,我正在尝试用它填充一些TextView。成功填充数据库后,我执行如下查询:
public Cursor getSubject(String dan, int ura, String oddelek){
String[] columns = new String[]{SQLiteHelper.PREDMET};
String selection = SQLiteHelper.DAN+"=? and "+SQLiteHelper.URA+"=? and "+SQLiteHelper.ODDELEK+"=?";
String[] selectionArgs = new String[]{dan, Integer.toString(ura), oddelek};
open();
return db.query(
SQLiteHelper.IME_TABELE,
columns,
selection,
selectionArgs,
null, null, null);
}
这是填充TextViews时的代码:
Cursor c = dbHandler.getSubject(dan, ura, razred);
if (!c.moveToFirst())
{
tw.setText("");
}
else {
String predmet = c.getString(c.getColumnIndex(SQLiteHelper.PREDMET));
String prostor = c.getString(c.getColumnIndex(SQLiteHelper.UCILNICA));
tw.setText(predmet);
ucilnica.setText(prostor);
}
所以你可以看到,我只是做一个查询,然后检查游标是否可以找到第一行,在找到它之后,我填充TextViews。奇怪的是它只适用于“PREDMET”而不适用于“UCILNICA”。当应用程序到达“UCILNICA”时,应用程序崩溃。
我得到的Logcat:
01-14 17:14:54.381: E/AndroidRuntime(3862): FATAL EXCEPTION: main
01-14 17:14:54.381: E/AndroidRuntime(3862): java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
01-14 17:14:54.381: E/AndroidRuntime(3862): at android.database.CursorWindow.nativeGetString(Native Method)
01-14 17:14:54.381: E/AndroidRuntime(3862): at android.database.CursorWindow.getString(CursorWindow.java:434)
01-14 17:14:54.381: E/AndroidRuntime(3862): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
01-14 17:14:54.381: E/AndroidRuntime(3862): at com.whizzapps.stpsurniki.Schedule.populateUrnik(Schedule.java:76)
01-14 17:14:54.381: E/AndroidRuntime(3862): at com.whizzapps.stpsurniki.Schedule$PopulateDatabase.onPostExecute(Schedule.java:218)
01-14 17:14:54.381: E/AndroidRuntime(3862): at com.whizzapps.stpsurniki.Schedule$PopulateDatabase.onPostExecute(Schedule.java:1)
01-14 17:14:54.381: E/AndroidRuntime(3862): at android.os.AsyncTask.finish(AsyncTask.java:631)
01-14 17:14:54.381: E/AndroidRuntime(3862): at android.os.AsyncTask.access$600(AsyncTask.java:177)
01-14 17:14:54.381: E/AndroidRuntime(3862): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
01-14 17:14:54.381: E/AndroidRuntime(3862): at android.os.Handler.dispatchMessage(Handler.java:99)
01-14 17:14:54.381: E/AndroidRuntime(3862): at android.os.Looper.loop(Looper.java:137)
01-14 17:14:54.381: E/AndroidRuntime(3862): at android.app.ActivityThread.main(ActivityThread.java:5103)
01-14 17:14:54.381: E/AndroidRuntime(3862): at java.lang.reflect.Method.invokeNative(Native Method)
01-14 17:14:54.381: E/AndroidRuntime(3862): at java.lang.reflect.Method.invoke(Method.java:525)
01-14 17:14:54.381: E/AndroidRuntime(3862): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
01-14 17:14:54.381: E/AndroidRuntime(3862): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-14 17:14:54.381: E/AndroidRuntime(3862): at dalvik.system.NativeStart.main(Native Method)
所以根据logcat,“UCILNICA”的索引是不对的,如果我是对的?但为什么不是呢?据我所知,我做的一切都是正确的。有趣的是,“PREDMET”和“UCILNICA”都在同一行并且都填充了,所以对我来说,一个人工作而一个人没有意义是没有意义的。
答案 0 :(得分:2)
我认为这是因为您错过了您的列中的ULCINICA列并传入查询。目前你只选择PREDMET。 尝试改变。
String[] columns = new String[]{SQLiteHelper.PREDMET};
到
String[] columns = new String[]{SQLiteHelper.PREDMET, SQLiteHelper.UCILNICA};