我正在查询联系信息以获取电话号码和电子邮件地址,这是我的代码:
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
}
}
}
我已经确保联系人有电话号码,但是在这些行中我仍然遇到空指针异常:
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)
我检查了引发异常的联系人,它有电话号码和电子邮件地址。我的代码有什么问题?