1

電話番号とメールアドレスを取得するために連絡先情報を照会しています。コードは次のとおりです。

protected void onPreExecute() {
    // get total count
    Cursor count = getContentResolver().query(
    ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
    total = count.getCount();

    // setup progress dialog
    progressDialog.setMessage(getString(R.string.profile_synchonizing));
    progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
    progressDialog.setProgress(0);
    progressDialog.setMax(total);
    progressDialog.show();        // Profile.java:270
}

protected Void doInBackground(String... arg0) {
    Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);

    while (cursor.moveToNext()) {
        String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
        String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

        if (Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
            Cursor phoneNumber = getContentResolver().query(Phone.CONTENT_URI, null, Phone.CONTACT_ID + " = ?", new String[] { contactId }, null);
            while (phoneNumber.moveToNext()) {
                // do something
            }
        }

        Cursor emails = getContentResolver().query(Email.CONTENT_URI, null, Email.CONTACT_ID + " = ?", new String[] { contactId }, null);
        while (emails.moveToNext()) {
            // do something
        }
    }
}

連絡先に電話番号があることを確認しましたが、これらの行で null ポインター例外が発生しました。

while (phoneNumber.moveToNext())     //Profile.java:315
while (emails.moveToNext())

例外スタック:

08-15 16:41:33.162: E/AndroidRuntime(20283): FATAL EXCEPTION: AsyncTask #2
08-15 16:41:33.162: E/AndroidRuntime(20283): java.lang.RuntimeException: An error occured while executing doInBackground()
08-15 16:41:33.162: E/AndroidRuntime(20283):    at android.os.AsyncTask$3.done(AsyncTask.java:200)
08-15 16:41:33.162: E/AndroidRuntime(20283):    at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
08-15 16:41:33.162: E/AndroidRuntime(20283):    at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
08-15 16:41:33.162: E/AndroidRuntime(20283):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
08-15 16:41:33.162: E/AndroidRuntime(20283):    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
08-15 16:41:33.162: E/AndroidRuntime(20283):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
08-15 16:41:33.162: E/AndroidRuntime(20283):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
08-15 16:41:33.162: E/AndroidRuntime(20283):    at java.lang.Thread.run(Thread.java:1027)
08-15 16:41:33.162: E/AndroidRuntime(20283): Caused by: java.lang.NullPointerException
08-15 16:41:33.162: E/AndroidRuntime(20283):    at test.Profile$SyncContactAsync.doInBackground(Profile.java:315)
08-15 16:41:33.162: E/AndroidRuntime(20283):    at test.Profile$SyncContactAsync.doInBackground(Profile.java:1)
08-15 16:41:33.162: E/AndroidRuntime(20283):    at android.os.AsyncTask$2.call(AsyncTask.java:185)
08-15 16:41:33.162: E/AndroidRuntime(20283):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
08-15 16:41:33.162: E/AndroidRuntime(20283):    ... 4 more
08-15 16:41:33.902: D/WindowManagerImpl(20283): finishRemoveViewLocked, mViews[0]: com.android.internal.policy.impl.PhoneWindow$DecorView@40545090
08-15 16:41:33.932: E/WindowManager(20283): Activity test.TabScreen has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@405bc868 that was originally added here
08-15 16:41:33.932: E/WindowManager(20283): android.view.WindowLeaked: Activity test.TabScreen has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@405bc868 that was originally added here
08-15 16:41:33.932: E/WindowManager(20283):     at android.view.ViewRoot.<init>(ViewRoot.java:278)
08-15 16:41:33.932: E/WindowManager(20283):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:149)
08-15 16:41:33.932: E/WindowManager(20283):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
08-15 16:41:33.932: E/WindowManager(20283):     at     android.view.Window$LocalWindowManager.addView(Window.java:433)
08-15 16:41:33.932: E/WindowManager(20283):     at android.app.Dialog.show(Dialog.java:265)
08-15 16:41:33.932: E/WindowManager(20283):     at test.Profile$SyncContactAsync.onPreExecute(Profile.java:270)
08-15 16:41:33.932: E/WindowManager(20283):     at android.os.AsyncTask.execute(AsyncTask.java:391)
08-15 16:41:33.932: E/WindowManager(20283):     at test.Profile$3.onItemClick(Profile.java:235)
08-15 16:41:33.932: E/WindowManager(20283):     at android.widget.AdapterView.performItemClick(AdapterView.java:284)
08-15 16:41:33.932: E/WindowManager(20283):     at android.widget.ListView.performItemClick(ListView.java:3569)
08-15 16:41:33.932: E/WindowManager(20283):     at android.widget.AbsListView$PerformClick.run(AbsListView.java:1831)
08-15 16:41:33.932: E/WindowManager(20283):     at android.os.Handler.handleCallback(Handler.java:587)
08-15 16:41:33.932: E/WindowManager(20283):     at android.os.Handler.dispatchMessage(Handler.java:92)
08-15 16:41:33.932: E/WindowManager(20283):     at android.os.Looper.loop(Looper.java:150)
08-15 16:41:33.932: E/WindowManager(20283):     at android.app.ActivityThread.main(ActivityThread.java:4385)
08-15 16:41:33.932: E/WindowManager(20283):     at java.lang.reflect.Method.invokeNative(Native Method)
08-15 16:41:33.932: E/WindowManager(20283):     at java.lang.reflect.Method.invoke(Method.java:507)
08-15 16:41:33.932: E/WindowManager(20283):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849)
08-15 16:41:33.932: E/WindowManager(20283):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
08-15 16:41:33.932: E/WindowManager(20283):     at dalvik.system.NativeStart.main(Native Method)

例外をスローした連絡先を確認しました。電話番号とメールアドレスが含まれています。コードの何が問題になっていますか?

4

3 に答える 3

2

moveToFirst()データをトラバースする前に、すべてのカーソルを呼び出す必要があります。

Cursor cursor = getContentResolver.query( ... );
cursor.moveToFirst();
while( cursor.moveToNext() ) {
    // process data here...
}

お役に立てれば...

于 2012-08-15T08:49:23.210 に答える
0

マニフェスト (AndroidManifest.xml) で連絡先の読み取り許可を忘れた可能性があります。

<uses-permission android:name="android.permission.READ_CONTACTS" />

注: これにより、NULL ポインター例外も発生します。

于 2012-08-15T09:00:39.087 に答える
0

カーソルを使用する前に null 値をチェックしてみませんか?
次に、クエリが結果を返さない理由を確認できます

于 2012-08-15T10:08:44.717 に答える