为了找到这个问题的答案,我一遍又一遍地搜索,但你是我唯一的机会。
这是事情:
我有一个 DBAdapter 类,MakeListActivity 和 SeeListActivity。
在 DBAdapter 我做了以下方法:
public Cursor commitQuery(String query)
{
return myDataBase.rawQuery(query,null);
}
MakeListActivity 有以下方法:
String query = "INSERT INTO list('name','description') VALUES('"+name+"','"+desc+"')";
dbAdapter.commitQuery(query);
最后,SeeListActivity 有以下代码:
Cursor c = dbAdapter.commitQuery("SELECT * FROM list");
if(c!=null)
{
c.moveToFirst();
Log.i("cursor",c.getString(1));
}
我实际上不确定我是否正确插入了数据,但是当我想查看数据(在我的情况下使用 logcat 记录它)时,我收到“不幸的是,com.dstankovic.app 已停止”。
这里也是 logcat 错误:
02-26 00:31:11.762: E/AndroidRuntime(660): java.lang.IllegalStateException: Could not execute method of the activity
02-26 00:31:11.762: E/AndroidRuntime(660): at android.view.View$1.onClick(View.java:3599)
02-26 00:31:11.762: E/AndroidRuntime(660): at android.view.View.performClick(View.java:4204)
02-26 00:31:11.762: E/AndroidRuntime(660): at android.view.View$PerformClick.run(View.java:17355)
02-26 00:31:11.762: E/AndroidRuntime(660): at android.os.Handler.handleCallback(Handler.java:725)
02-26 00:31:11.762: E/AndroidRuntime(660): at android.os.Handler.dispatchMessage(Handler.java:92)
02-26 00:31:11.762: E/AndroidRuntime(660): at android.os.Looper.loop(Looper.java:137)
02-26 00:31:11.762: E/AndroidRuntime(660): at android.app.ActivityThread.main(ActivityThread.java:5041)
02-26 00:31:11.762: E/AndroidRuntime(660): at java.lang.reflect.Method.invokeNative(Native Method)
02-26 00:31:11.762: E/AndroidRuntime(660): at java.lang.reflect.Method.invoke(Method.java:511)
02-26 00:31:11.762: E/AndroidRuntime(660): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-26 00:31:11.762: E/AndroidRuntime(660): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-26 00:31:11.762: E/AndroidRuntime(660): at dalvik.system.NativeStart.main(Native Method)
02-26 00:31:11.762: E/AndroidRuntime(660): Caused by: java.lang.reflect.InvocationTargetException
02-26 00:31:11.762: E/AndroidRuntime(660): at java.lang.reflect.Method.invokeNative(Native Method)
02-26 00:31:11.762: E/AndroidRuntime(660): at java.lang.reflect.Method.invoke(Method.java:511)
02-26 00:31:11.762: E/AndroidRuntime(660): at android.view.View$1.onClick(View.java:3594)
02-26 00:31:11.762: E/AndroidRuntime(660): ... 11 more
02-26 00:31:11.762: E/AndroidRuntime(660): Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
02-26 00:31:11.762: E/AndroidRuntime(660): at android.database.AbstractCursor.checkPosition(AbstractCursor.java:424)
02-26 00:31:11.762: E/AndroidRuntime(660): at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
02-26 00:31:11.762: E/AndroidRuntime(660): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
02-26 00:31:11.762: E/AndroidRuntime(660): at com.dulerock.eatwell.MakeNewListActivity.SaveList(MakeNewListActivity.java:278)
有人可以给我一个提示吗?
提前致谢
最佳答案
这里游标不为空但它没有数据......你需要确保游标中有数据moveToFirst()
如果游标中有数据则返回真,如果为空则返回假
试试这个
Cursor c = dbAdapter.commitQuery("SELECT * FROM list");
if(c.moveToFirst())
{
Log.i("cursor",c.getString(1));
}