从安卓中的联系人加载器获取电话号码:投影崩溃



我正在使用Google教程中的示例代码。投影是我指定要收集的数据的位置。如何指定我想要电话号码?来自谷歌的样本没有获得电话号码。但是我想获取电话号码,所以我添加了Phone.NUMBER字段。但是当我这样做时,应用程序总是崩溃。这是我的预测。

// 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,
            // 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,
            // Contacts.HAS_PHONE_NUMBER,
            //
            CommonDataKinds.Phone.NUMBER,
        };
        // 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 PHOTO_THUMBNAIL_DATA = 3;
        final static int SORT_KEY = 4;
        // final static int HAS_PHONE_NUMBER = 5;
        final static int PHONE_NUMBER = 5;
    }

这是错误跟踪

12-18 10:20:54.299: E/AndroidRuntime(1909): FATAL EXCEPTION: ModernAsyncTask #1
12-18 10:20:54.299: E/AndroidRuntime(1909): java.lang.RuntimeException: An error occured while executing doInBackground()
12-18 10:20:54.299: E/AndroidRuntime(1909):     at android.support.v4.content.ModernAsyncTask$3.done(ModernAsyncTask.java:137)
12-18 10:20:54.299: E/AndroidRuntime(1909):     at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
12-18 10:20:54.299: E/AndroidRuntime(1909):     at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
12-18 10:20:54.299: E/AndroidRuntime(1909):     at java.util.concurrent.FutureTask.run(FutureTask.java:239)
12-18 10:20:54.299: E/AndroidRuntime(1909):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
12-18 10:20:54.299: E/AndroidRuntime(1909):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
12-18 10:20:54.299: E/AndroidRuntime(1909):     at java.lang.Thread.run(Thread.java:841)
12-18 10:20:54.299: E/AndroidRuntime(1909): Caused by: java.lang.IllegalArgumentException: Invalid column data1
12-18 10:20:54.299: E/AndroidRuntime(1909):     at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:167)
12-18 10:20:54.299: E/AndroidRuntime(1909):     at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:137)
12-18 10:20:54.299: E/AndroidRuntime(1909):     at android.content.ContentProviderProxy.query(ContentProviderNative.java:385)
12-18 10:20:54.299: E/AndroidRuntime(1909):     at android.content.ContentResolver.query(ContentResolver.java:417)
12-18 10:20:54.299: E/AndroidRuntime(1909):     at android.content.ContentResolver.query(ContentResolver.java:360)
12-18 10:20:54.299: E/AndroidRuntime(1909):     at android.support.v4.content.CursorLoader.loadInBackground(CursorLoader.java:49)
12-18 10:20:54.299: E/AndroidRuntime(1909):     at android.support.v4.content.CursorLoader.loadInBackground(CursorLoader.java:35)
12-18 10:20:54.299: E/AndroidRuntime(1909):     at android.support.v4.content.AsyncTaskLoader.onLoadInBackground(AsyncTaskLoader.java:242)
12-18 10:20:54.299: E/AndroidRuntime(1909):     at android.support.v4.content.AsyncTaskLoader$LoadTask.doInBackground(AsyncTaskLoader.java:51)
12-18 10:20:54.299: E/AndroidRuntime(1909):     at android.support.v4.content.AsyncTaskLoader$LoadTask.doInBackground(AsyncTaskLoader.java:40)
12-18 10:20:54.299: E/AndroidRuntime(1909):     at android.support.v4.content.ModernAsyncTask$2.call(ModernAsyncTask.java:123)
12-18 10:20:54.299: E/AndroidRuntime(1909):     at java.util.concurrent.FutureTask.run(FutureTask.java:234)
12-18 10:20:54.299: E/AndroidRuntime(1909):     ... 3 more

这是因为此 URI 不支持获取CommonDataKinds.Phone.NUMBER字段。您应该向CommonDataKinds.Phone.CONTENT_URI提出请求。看看这个答案,在获取联系人姓名和电话的同时使用此URI。

最新更新