0
我是android新手,試圖使用下面顯示的代碼獲取特定號碼的sms對話。這段代碼導致了一堆錯誤。Android:無法從CursorWindow讀取第0行,第-1列。在從它訪問數據之前確保光標已正確初始化
任何人可以幫助我如何解決這個問題?
String add = "address="+"'"+number+"'";
Uri uriSms = Uri.parse("content://mms-sms/conversations?simple=true");
String[] projection = new String[]{"_id", "address", "person", "body", "date", "type"};
Cursor cursor = getContentResolver().query(uriSms,projection ,add,null,"date desc");
if (cursor.moveToFirst()) {
int index_Address = cursor.getColumnIndex("address");
int index_Person = cursor.getColumnIndex("person");
int index_Body = cursor.getColumnIndex("body");
logcat的
04-02 13:21:14.912: E/CursorWindow(836): Failed to read row 0, column -1 from a CursorWindow which has 16 rows, 16 columns.
04-02 13:21:14.922: E/AndroidRuntime(836): FATAL EXCEPTION: main
04-02 13:21:14.922: E/AndroidRuntime(836): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.second/com.example.second.CInfo}: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
04-02 13:21:14.922: E/AndroidRuntime(836): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2114)
04-02 13:21:14.922: E/AndroidRuntime(836): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2139)
04-02 13:21:14.922: E/AndroidRuntime(836): at android.app.ActivityThread.access$700(ActivityThread.java:143)
04-02 13:21:14.922: E/AndroidRuntime(836): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1241)
04-02 13:21:14.922: E/AndroidRuntime(836): at android.os.Handler.dispatchMessage(Handler.java:99)
04-02 13:21:14.922: E/AndroidRuntime(836): at android.os.Looper.loop(Looper.java:137)
04-02 13:21:14.922: E/AndroidRuntime(836): at android.app.ActivityThread.main(ActivityThread.java:4960)
04-02 13:21:14.922: E/AndroidRuntime(836): at java.lang.reflect.Method.invokeNative(Native Method)
04-02 13:21:14.922: E/AndroidRuntime(836): at java.lang.reflect.Method.invoke(Method.java:511)
04-02 13:21:14.922: E/AndroidRuntime(836): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
04-02 13:21:14.922: E/AndroidRuntime(836): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
04-02 13:21:14.922: E/AndroidRuntime(836): at dalvik.system.NativeStart.main(Native Method)
04-02 13:21:14.922: E/AndroidRuntime(836): Caused by: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
04-02 13:21:14.922: E/AndroidRuntime(836): at android.database.CursorWindow.nativeGetString(Native Method)
04-02 13:21:14.922: E/AndroidRuntime(836): at android.database.CursorWindow.getString(CursorWindow.java:438)
04-02 13:21:14.922: E/AndroidRuntime(836): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
04-02 13:21:14.922: E/AndroidRuntime(836): at android.database.CursorWrapper.getString(CursorWrapper.java:114)
04-02 13:21:14.922: E/AndroidRuntime(836): at com.example.second.CInfo.fetchConversations(CInfo.java:96)
04-02 13:21:14.922: E/AndroidRuntime(836): at com.example.second.CInfo.onCreate(CInfo.java:54)
04-02 13:21:14.922: E/AndroidRuntime(836): at android.app.Activity.performCreate(Activity.java:5203)
04-02 13:21:14.922: E/AndroidRuntime(836): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
04-02 13:21:14.922: E/AndroidRuntime(836): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2078)
此錯誤主要出現在查詢寫錯了。確保數據庫的字段名稱與您在查詢中編寫的字段名稱匹配。 – Dhaval
感謝您的重播,但是當我使用uri = content:// mms-sms /對話時insted的內容:// mms-sms /對話?simple = true並且在模擬器中運行時它工作正常,但是當我在我的移動它給了我上面的錯誤。 – user2423768
是的,你是對的Dhaval我再次得到了問題。 – user2423768