获取无效的列数据1?

时间:2015-04-27 12:31:17

标签: android android-cursor

我试图获取用户图片,联系人姓名和联系电话,但收到异常无效的列data1错误。

这是我的代码:

public interface ContactsQuery {

    // An identifier for the loader
    final static int QUERY_ID = 1;

    // A content URI for the Contacts table
    final static Uri CONTENT_URI = Contacts.CONTENT_URI;

    // The search/filter query Uri
    final static Uri FILTER_URI = Contacts.CONTENT_FILTER_URI;

    // The selection clause for the CursorLoader query. The search criteria defined here
    // restrict results to contacts that have a display name and are linked to visible groups.
    // Notice that the search on the string provided by the user is implemented by appending
    // the search string to CONTENT_FILTER_URI.
    @SuppressLint("InlinedApi")
    final static String SELECTION =
            (Utils.hasHoneycomb() ? Contacts.DISPLAY_NAME_PRIMARY : Contacts.DISPLAY_NAME) +
            "<>''" + " AND " + Contacts.IN_VISIBLE_GROUP + "=1";

    // The desired sort order for the returned Cursor. In Android 3.0 and later, the primary
    // sort key allows for localization. In earlier versions. use the display name as the sort
    // key.
    @SuppressLint("InlinedApi")
    final static String SORT_ORDER =
            Utils.hasHoneycomb() ? Contacts.SORT_KEY_PRIMARY : Contacts.DISPLAY_NAME;

    // The projection for the CursorLoader query. This is a list of columns that the Contacts
    // Provider should return in the Cursor.
    @SuppressLint("InlinedApi")
    final static String[] PROJECTION = {

            // The contact's row id
            Contacts._ID,

            // A pointer to the contact that is guaranteed to be more permanent than _ID. Given
            // a contact's current _ID value and LOOKUP_KEY, the Contacts Provider can generate
            // a "permanent" contact URI.
            Contacts.LOOKUP_KEY,

            // In platform version 3.0 and later, the Contacts table contains
            // DISPLAY_NAME_PRIMARY, which either contains the contact's displayable name or
            // some other useful identifier such as an email address. This column isn't
            // available in earlier versions of Android, so you must use Contacts.DISPLAY_NAME
            // instead.
            Utils.hasHoneycomb() ? Contacts.DISPLAY_NAME_PRIMARY : Contacts.DISPLAY_NAME,

            Utils.hasHoneycomb() ? ContactsContract.CommonDataKinds.Phone.NUMBER : ContactsContract.CommonDataKinds.Phone.NUMBER,

            // In Android 3.0 and later, the thumbnail image is pointed to by
            // PHOTO_THUMBNAIL_URI. In earlier versions, there is no direct pointer; instead,
            // you generate the pointer from the contact's ID value and constants defined in
            // android.provider.ContactsContract.Contacts.
            Utils.hasHoneycomb() ? Contacts.PHOTO_THUMBNAIL_URI : Contacts._ID,

            // The sort order column for the returned Cursor, used by the AlphabetIndexer
            SORT_ORDER,
    };

    // The query column numbers which map to each value in the projection
    final static int ID = 0;
    final static int LOOKUP_KEY = 1;
    final static int DISPLAY_NAME = 2;
    final static int DISPLAY_NUMBER = 3;
    final static int PHOTO_THUMBNAIL_DATA = 4;
    final static int SORT_KEY = 5;
}

这是我的例外:

04-27 17:53:19.770: E/AndroidRuntime(21494): FATAL EXCEPTION: ModernAsyncTask #1
04-27 17:53:19.770: E/AndroidRuntime(21494): Process: com.newtglobal.fmfm, PID: 21494
04-27 17:53:19.770: E/AndroidRuntime(21494): java.lang.RuntimeException: An error occured while executing doInBackground()
04-27 17:53:19.770: E/AndroidRuntime(21494):    at android.support.v4.content.ModernAsyncTask$3.done(ModernAsyncTask.java:137)
04-27 17:53:19.770: E/AndroidRuntime(21494):    at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
04-27 17:53:19.770: E/AndroidRuntime(21494):    at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
04-27 17:53:19.770: E/AndroidRuntime(21494):    at java.util.concurrent.FutureTask.run(FutureTask.java:242)
04-27 17:53:19.770: E/AndroidRuntime(21494):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
04-27 17:53:19.770: E/AndroidRuntime(21494):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
04-27 17:53:19.770: E/AndroidRuntime(21494):    at java.lang.Thread.run(Thread.java:841)
04-27 17:53:19.770: E/AndroidRuntime(21494): Caused by: java.lang.IllegalArgumentException: Invalid column data1
04-27 17:53:19.770: E/AndroidRuntime(21494):    at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:167)
04-27 17:53:19.770: E/AndroidRuntime(21494):    at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:137)
04-27 17:53:19.770: E/AndroidRuntime(21494):    at android.content.ContentProviderProxy.query(ContentProviderNative.java:413)
04-27 17:53:19.770: E/AndroidRuntime(21494):    at android.content.ContentResolver.query(ContentResolver.java:461)
04-27 17:53:19.770: E/AndroidRuntime(21494):    at android.content.ContentResolver.query(ContentResolver.java:404)
04-27 17:53:19.770: E/AndroidRuntime(21494):    at android.support.v4.content.CursorLoader.loadInBackground(CursorLoader.java:49)
04-27 17:53:19.770: E/AndroidRuntime(21494):    at android.support.v4.content.CursorLoader.loadInBackground(CursorLoader.java:35)
04-27 17:53:19.770: E/AndroidRuntime(21494):    at android.support.v4.content.AsyncTaskLoader.onLoadInBackground(AsyncTaskLoader.java:242)
04-27 17:53:19.770: E/AndroidRuntime(21494):    at android.support.v4.content.AsyncTaskLoader$LoadTask.doInBackground(AsyncTaskLoader.java:51)
04-27 17:53:19.770: E/AndroidRuntime(21494):    at android.support.v4.content.AsyncTaskLoader$LoadTask.doInBackground(AsyncTaskLoader.java:40)
04-27 17:53:19.770: E/AndroidRuntime(21494):    at android.support.v4.content.ModernAsyncTask$2.call(ModernAsyncTask.java:123)
04-27 17:53:19.770: E/AndroidRuntime(21494):    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
04-27 17:53:19.770: E/AndroidRuntime(21494):    ... 3 more

任何帮助对我都有帮助。在此先感谢朋友们。

1 个答案:

答案 0 :(得分:0)

首先检查游标是否为空,如果不为空,则查看游标具有的所有列

假设c是游标对象

    if(c !=null)
      {
         String temp[]=c.getColumnNames();
      }

添加一些日志记录并查看列名称,位置和值。 希望这会对你有帮助.BTW添加try / catch其良好的编程习惯。

相关问题