Android:无法从CursorWindow读取第0行col -1。在从中访问数据之前,请确保正确初始化Cursor

时间:2014-04-02 08:10:26

标签: android sms

我是android的新手,并尝试使用下面显示的代码获取特定号码的短信对话。此代码会导致一堆错误。

任何人都可以帮我解决这个问题吗?

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)

1 个答案:

答案 0 :(得分: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构造函数要求您传入要返回的游标中的字符串数组。如果我尝试访问say列OutletsContentProvider.KEY_I_DONT_EXIST,则会抛出此异常。

我希望这会有所帮助。 :)