Get phone number from contact loader in android: projection crash

人走茶凉 提交于 2019-12-11 13:33:38

问题


I am using the sample code from the Google tutorial. The projection is where I specify the data I want to collect. How do I specify that I want the phone numbers? The sample from Google does not get phone number. But I want to obtain the phone number, so I add the field for Phone.NUMBER. But when I do, the app always crashes. Here is my projection.

// 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;
    }

Here is the error trace

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

回答1:


Thats because this URI does not support getting CommonDataKinds.Phone.NUMBER field. You should make a request to CommonDataKinds.Phone.CONTENT_URI. Take a look at this answer to make use of this URI while getting both contact name and phone.



来源:https://stackoverflow.com/questions/20665392/get-phone-number-from-contact-loader-in-android-projection-crash

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!