这是录像机的项目。我遇到的唯一问题是从sqllite检索存储的uri视频。如果我不调用retrieveVid()方法,项目工作正常但是当调用retrieveVid()方法时它崩溃了。我正在尝试检索存储在sqllite数据库的第1列和第2行的uri。内部checkEvents()方法我已经评论了我尝试过的其他代码。问题可能在checkEvents()或getEvents()方法或retrieveVid()方法。因为我在移动设备上运行项目,因为相机在我的模拟器上不起作用所以我没有logcat.I我没有发布SecondActvity.java因为它与数据库或我的问题无关。
logcat的
06-25 21:36:24.501: E/CursorWindow(23206): Failed to read row 2, column 1 from a CursorWindow which has 123 rows, 1 columns.
06-25 21:36:24.504: D/AndroidRuntime(23206): Shutting down VM
06-25 21:36:24.504: W/dalvikvm(23206): threadid=1: thread exiting with uncaught exception (group=0x40e89908)
06-25 21:36:24.514: E/AndroidRuntime(23206): FATAL EXCEPTION: main
06-25 21:36:24.514: E/AndroidRuntime(23206): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=200, result=-1, data=Intent { dat=file:///storage/sdcard0/Pictures/Hello Camera/VID_20140625_213611.mp4 }} to activity {com.example.Assignment/com.example.Assignment.MainActivity}: java.lang.IllegalStateException: Couldn't read row 2, col 1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
06-25 21:36:24.514: E/AndroidRuntime(23206): at android.app.ActivityThread.deliverResults(ActivityThread.java:3302)
06-25 21:36:24.514: E/AndroidRuntime(23206): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3345)
06-25 21:36:24.514: E/AndroidRuntime(23206): at android.app.ActivityThread.access$1100(ActivityThread.java:149)
06-25 21:36:24.514: E/AndroidRuntime(23206): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1348)
06-25 21:36:24.514: E/AndroidRuntime(23206): at android.os.Handler.dispatchMessage(Handler.java:99)
06-25 21:36:24.514: E/AndroidRuntime(23206): at android.os.Looper.loop(Looper.java:153)
06-25 21:36:24.514: E/AndroidRuntime(23206): at android.app.ActivityThread.main(ActivityThread.java:5086)
06-25 21:36:24.514: E/AndroidRuntime(23206): at java.lang.reflect.Method.invokeNative(Native Method)
06-25 21:36:24.514: E/AndroidRuntime(23206): at java.lang.reflect.Method.invoke(Method.java:511)
06-25 21:36:24.514: E/AndroidRuntime(23206): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
06-25 21:36:24.514: E/AndroidRuntime(23206): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
06-25 21:36:24.514: E/AndroidRuntime(23206): at dalvik.system.NativeStart.main(Native Method)
06-25 21:36:24.514: E/AndroidRuntime(23206): Caused by: java.lang.IllegalStateException: Couldn't read row 2, col 1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
06-25 21:36:24.514: E/AndroidRuntime(23206): at android.database.CursorWindow.nativeGetString(Native Method)
06-25 21:36:24.514: E/AndroidRuntime(23206): at android.database.CursorWindow.getString(CursorWindow.java:434)
06-25 21:36:24.514: E/AndroidRuntime(23206): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
06-25 21:36:24.514: E/AndroidRuntime(23206): at com.example.Assignment.MainActivity.checkEvents(MainActivity.java:367)
06-25 21:36:24.514: E/AndroidRuntime(23206): at com.example.Assignment.MainActivity.retrieveVid(MainActivity.java:374)
06-25 21:36:24.514: E/AndroidRuntime(23206): at com.example.Assignment.MainActivity.onActivityResult(MainActivity.java:321)
06-25 21:36:24.514: E/AndroidRuntime(23206): at android.app.Activity.dispatchActivityResult(Activity.java:5204)
06-25 21:36:24.514: E/AndroidRuntime(23206): at android.app.ActivityThread.deliverResults(ActivityThread.java:3298)
06-25 21:36:24.514: E/AndroidRuntime(23206): ... 11 more
答案 0 :(得分:1)
列索引从0开始。您的表只有1列。您没有指定投影,因此在游标中返回所有列。尝试在列索引1处检索值会导致异常。
要解决此问题,请将getString(1)
更改为getString(0)
。
答案 1 :(得分:1)
首先,您尝试读取光标的第二行,而不检查它是否存在。另外,就像@laalto所说,光标上的列是从零开始的,因此使用'getString(1)'你将得不到任何东西,因为该表只有一列。
我建议你像这样获取String,这将保存,即使你向表中添加更多行:
if(cursor.moveToPosition(2)){
a = cursor.getString(cursor.getColumnIndex(sql.COLUMN_VID));
// add a constant (COLUMN_VID) to your sql class which defines the row name and also use this constant in your table creation
}