2

嗨,我正在使用 Loader 来获取一些数据。代码如下。

    @Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {

    return new CursorLoader(this, ContactsContract.PhoneLookup.CONTENT_FILTER_URI,
            new String[] { PhoneLookup.TYPE, PhoneLookup.NUMBER,
                    PhoneLookup.DISPLAY_NAME, PhoneLookup._ID,
                    PhoneLookup.PHOTO_ID }, null, null, null);

}

我遇到了一个例外。有人能帮我吗

droidRuntime( 2047): java.lang.RuntimeException: An error occured while executing doInBackground()
    E/AndroidRuntime( 2047):    at android.os.AsyncTask$3.done(AsyncTask.java:278)
    E/AndroidRuntime( 2047):    at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
    E/AndroidRuntime( 2047):    at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
    E/AndroidRuntime( 2047):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
    E/AndroidRuntime( 2047):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
    E/AndroidRuntime( 2047):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
    E/AndroidRuntime( 2047):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
    E/AndroidRuntime( 2047):    at java.lang.Thread.run(Thread.java:856)
    E/AndroidRuntime( 2047): Caused by: java.lang.IllegalArgumentException: Invalid column data1
    E/AndroidRuntime( 2047):    at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:166)
    E/AndroidRuntime( 2047):    at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:136)
    E/AndroidRuntime( 2047):    at android.content.ContentProviderProxy.query(ContentProviderNative.java:358)
    E/AndroidRuntime( 2047):    at android.content.ContentResolver.query(ContentResolver.java:311)
    E/AndroidRuntime( 2047):    at android.content.CursorLoader.loadInBackground(CursorLoader.java:56)
    E/AndroidRuntime( 2047):    at android.content.CursorLoader.loadInBackground(CursorLoader.java:42)
    E/AndroidRuntime( 2047):    at android.content.AsyncTaskLoader.onLoadInBackground(AsyncTaskLoader.java:255)
    E/AndroidRuntime( 2047):    at android.content.AsyncTaskLoader$LoadTask.doInBackground(AsyncTaskLoader.java:66)
    E/AndroidRuntime( 2047):    at android.content.AsyncTaskLoader$LoadTask.doInBackground(AsyncTaskLoader.java:55)
    E/AndroidRuntime( 2047):    at android.os.AsyncTask$2.call(AsyncTask.java:264)
    E/AndroidRuntime( 2047):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
    E/AndroidRuntime( 2047):    ... 4 more

当我使用下面的代码时它工作正常

    @Override
public Loader<Cursor> onCreateLoader(int id, Bundle args)
{

    return new CursorLoader(this, ContactsContract.Contacts.CONTENT_URI,
            new String[]
            { ContactsContract.Contacts._ID,
                    ContactsContract.Contacts.DISPLAY_NAME,
                    ContactsContract.Contacts.LAST_TIME_CONTACTED }, null,
            null, null);

}
4

1 回答 1

0

您可以使用以下代码从电话簿中检索联系人。

   public ArrayList<String> ColletContact()
{ 
    stringName = new ArrayList<String>();
    stringPhoneNo = new ArrayList<String>();

    ContentResolver cr = getContentResolver();
    Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
            null, null, null, null);
    if (cur.getCount() > 0) {
    while (cur.moveToNext()) {
        String id = cur.getString(
                    cur.getColumnIndex(ContactsContract.Contacts._ID));
    String name = cur.getString(
                    cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
    if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
        //Query phone here.  Covered next

        Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", 
                new String[]{id}, null);
                while (pCur.moveToNext()) {
                // Do something with phones
                    String phoneNo = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

                    stringName.add(name);
                                    stringPhoneNo.add(phoneNo):

                } 
                pCur.close();                     
        }
     }

   }      
    return stringName;      
} 
于 2012-07-27T06:13:50.263 回答