2014-04-02 108 views
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) 
+0

此錯誤主要出現在查詢寫錯了。確保數據庫的字段名稱與您在查詢中編寫的字段名稱匹配。 – Dhaval

+0

感謝您的重播,但是當我使用uri = content:// mms-sms /對話時insted的內容:// mms-sms /對話?simple = true並且在模擬器中運行時它工作正常,但是當我在我的移動它給了我上面的錯誤。 – user2423768

+0

是的,你是對的Dhaval我再次得到了問題。 – user2423768

回答

0

請確保列嘗試由存在於由內容提供商返回的光標顯示數據。

如果它嘗試訪問不存在的列,通常會拋出此異常。因此,檢查你傳遞到內容解析器在這種情況下,突出/選擇:

例如,在一個典型的onCreateLoader方法:

public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) { 
    Log.i(TAG, "onCreateLoader running..."); 

    String[] projection = new String[]{ 
      OutletsContentProvider.KEY_ID, 
      OutletsContentProvider.KEY_OUTLET_ICON, 
      OutletsContentProvider.KEY_OUTLET_NAME, 
      OutletsContentProvider.KEY_OUTLET_LONGITUDE, 
      OutletsContentProvider.KEY_OUTLET_LATITUDE, 
      OutletsContentProvider.KEY_OUTLET_ADDRESS, 
      OutletsContentProvider.KEY_OUTLET_PHONE, 
      OutletsContentProvider.KEY_OUTLET_EMAIL    
    }; 

    CursorLoader loader = new CursorLoader(this, OutletsContentProvider.CONTENT_URI, projection,null,null, null); 

    Log.i(TAG, "onCreateLoader complete"); 

    return loader; 
} 

的cusorloader構造希望你在列中的一個字符串數組傳遞遊標將被返回。如果我嘗試訪問列說OutletsContentProvider.KEY_I_DONT_EXIST,這個異常將被拋出。

我希望這會有所幫助。 :)

相關問題