我是android新手,所以学习AsyncTask库。 我想在AsyncTask中对用户联系人列表执行操作。因为这是一个漫长的过程,我不希望用户等到它完成。但每当我运行它,它关闭我的应用程序,不调用getContentResolver方法,我的AsyncTask方法工作正常。 我不知道我的getContentResolver方法有什么问题。
我的AsyncTask文件是,
public class clsAsyncTask extends AsyncTask<TextView, String, Boolean> {
TextView txtView;
Boolean finalStatus = false;
@Override
protected Boolean doInBackground(TextView... params) {
if(params.length > 0){
txtView = params[0];
saveContacts contacts = new saveContacts("");
contacts.onHandleIntent(new Intent());
publishProgress("Step 2 cleared");
finalStatus = true;
}
return finalStatus;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected void onPostExecute(Boolean aBoolean) {
if(aBoolean){
publishProgress("All cleared");
}else{
publishProgress("Error accured");
}
}
@Override
protected void onProgressUpdate(String... values) {
txtView.setText(values[0]);
}
}
我的getContentResolver文件是,
public class saveContacts extends IntentService {
public saveContacts(String name) {
super(name);
}
@Override
protected void onHandleIntent(Intent intent) {
StringBuffer sb = new StringBuffer();
sb.append("......Contact Details.....");
String name = "";
String phoneNumber="";
String emailId = "";
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
sb.append("total = " + phones.getCount());
while (phones.moveToNext())
{
name=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
sb.append("\nEmail: " + name + " phoneNumber: " + phoneNumber);
}
phones.close();
}
}
我的主要活动是调用Async方法,如下所示,
public class MainActivity extends ActionBarActivity {
TextView txtView;
clsAsyncTask _AsyncTaskWorker;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtView = (TextView) findViewById(R.id.txt_view);
txtView.setText("Execution start");
_AsyncTaskWorker = new clsAsyncTask();
_AsyncTaskWorker.execute(txtView);
}
}
Logcat错误部分,它显示如下,
07-11 09:02:24.630 664-705/? E/ElectronBeam﹕ Can not update rotation!
07-11 09:02:24.946 664-705/? E/ElectronBeam﹕ Can not update rotation!
07-11 09:02:26.295 4384-4384/? E/InitDefaultSoftInfo﹕ i=1defaultresolveinfo.size():2
07-11 09:02:26.295 4384-4384/? E/InitDefaultSoftInfo﹕ best != null
07-11 09:02:26.327 735-735/? E/KeyguardUpdateMonitor﹕ Object tried to add another callback
java.lang.Exception: Called by
at com.android.keyguard.KeyguardUpdateMonitor.registerCallback(KeyguardUpdateMonitor.java:1176)
at com.android.keyguard.KeyguardSelectorView.onResume(KeyguardSelectorView.java:397)
at com.android.keyguard.KeyguardHostView.onScreenTurnedOn(KeyguardHostView.java:1311)
at com.android.keyguard.KeyguardViewManager.onScreenTurnedOn(KeyguardViewManager.java:576)
at com.android.keyguard.KeyguardViewMediator.handleNotifyScreenOn(KeyguardViewMediator.java:1933)
at com.android.keyguard.KeyguardViewMediator.access$2500(KeyguardViewMediator.java:132)
at com.android.keyguard.KeyguardViewMediator$4.handleMessage(KeyguardViewMediator.java:1539)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5341)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:830)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:646)
at dalvik.system.NativeStart.main(Native Method)
07-11 09:02:26.343 4384-4384/? E/InitDefaultSoftInfo﹕ i=2defaultresolveinfo.size():3
07-11 09:02:26.343 4384-4384/? E/InitDefaultSoftInfo﹕ best != null
07-11 09:02:26.351 4384-4384/? E/InitDefaultSoftInfo﹕ i=3defaultresolveinfo.size():4
07-11 09:02:26.351 4384-4384/? E/InitDefaultSoftInfo﹕ best != null
07-11 09:02:26.356 664-664/? E/﹕ Could not open '/data/data/hotplug/cmd'
07-11 09:02:26.356 664-664/? E/﹕ error : 2, No such file or directory
07-11 09:02:26.440 4384-4384/? E/InitDefaultSoftInfo﹕ i=4defaultresolveinfo.size():5
07-11 09:02:26.440 4384-4384/? E/InitDefaultSoftInfo﹕ best != null
07-11 09:02:26.700 4384-4384/? E/InitDefaultSoftInfo﹕ i=6defaultresolveinfo.size():7
07-11 09:02:26.700 4384-4384/? E/InitDefaultSoftInfo﹕ best != null
07-11 09:02:26.814 4384-4384/? E/InitDefaultSoftInfo﹕ i=7defaultresolveinfo.size():8
07-11 09:02:26.814 4384-4384/? E/InitDefaultSoftInfo﹕ best != null
07-11 09:02:26.944 143-30185/? E/AudioMTKHardware﹕ setCommonParameters() still have param.size() = 1, remain param = "screen_state=on"
07-11 09:02:28.333 664-664/? E/RemoteViews﹕ ANR Warning,RemoteViews can only be used once ,if not ,it may cause ANR in hosts such as Laucher,SystemUI. keys for search <ANR Exception MSG History>
07-11 09:02:28.802 858-1274/? E/CellLocation﹕ create GsmCellLocation
07-11 09:02:29.049 858-1274/? E/CellLocation﹕ create GsmCellLocation
07-11 09:02:29.514 973-1156/? E/ActivityThread﹕ Failed to find provider info for com.gionee.account
07-11 09:02:30.202 16221-16221/? E/PhotosPlugin﹕ Loading PhotosPlugin
07-11 09:02:31.962 143-516/? E/﹕ AudioCloseDumpPCMFile file== NULL
07-11 09:02:31.962 143-516/? E/﹕ AudioCloseDumpPCMFile file== NULL
07-11 09:02:31.962 143-516/? E/﹕ AudioCloseDumpPCMFile file== NULL
07-11 09:02:31.962 143-516/? E/﹕ AudioCloseDumpPCMFile file== NULL
07-11 09:02:32.476 16165-16241/keval.asynctask E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1
Process: keval.asynctask, PID: 16165
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:300)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:841)
Caused by: java.lang.NullPointerException
at android.content.ContextWrapper.getContentResolver(ContextWrapper.java:99)
at keval.asynctask.saveContacts.onHandleIntent(saveContacts.java:26)
at keval.asynctask.clsAsyncTask.doInBackground(clsAsyncTask.java:24)
at keval.asynctask.clsAsyncTask.doInBackground(clsAsyncTask.java:14)
at android.os.AsyncTask$2.call(AsyncTask.java:288)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:841)
07-11 09:02:32.501 16165-16165/keval.asynctask E/﹕ appName=keval.asynctask, acAppName=/system/bin/surfaceflinger
07-11 09:02:32.501 16165-16165/keval.asynctask E/﹕ 0
07-11 09:02:32.501 16165-16165/keval.asynctask E/﹕ appName=keval.asynctask, acAppName=/system/bin/surfaceflinger
07-11 09:02:32.501 16165-16165/keval.asynctask E/﹕ 0
07-11 09:02:32.889 664-684/? E/﹕ Could not open '/data/data/hotplug/cmd'
07-11 09:02:32.889 664-684/? E/﹕ error : 2, No such file or directory
07-11 09:02:33.077 4384-4384/? E/InitDefaultSoftInfo﹕ i=1defaultresolveinfo.size():2
07-11 09:02:33.077 4384-4384/? E/InitDefaultSoftInfo﹕ best != null
07-11 09:02:33.093 4384-4384/? E/InitDefaultSoftInfo﹕ i=2defaultresolveinfo.size():3
07-11 09:02:33.093 4384-4384/? E/InitDefaultSoftInfo﹕ best != null
07-11 09:02:33.095 4384-4384/? E/InitDefaultSoftInfo﹕ i=3defaultresolveinfo.size():4
07-11 09:02:33.095 4384-4384/? E/InitDefaultSoftInfo﹕ best != null
07-11 09:02:33.096 4384-4384/? E/InitDefaultSoftInfo﹕ i=4defaultresolveinfo.size():5
07-11 09:02:33.096 4384-4384/? E/InitDefaultSoftInfo﹕ best != null
07-11 09:02:33.141 4384-4384/? E/InitDefaultSoftInfo﹕ i=6defaultresolveinfo.size():7
07-11 09:02:33.141 4384-4384/? E/InitDefaultSoftInfo﹕ best != null
07-11 09:02:33.163 4384-4384/? E/InitDefaultSoftInfo﹕ i=7defaultresolveinfo.size():8
07-11 09:02:33.163 4384-4384/? E/InitDefaultSoftInfo﹕ best != null
07-11 09:02:33.496 664-944/? E/﹕ Could not open '/data/data/hotplug/cmd'
07-11 09:02:33.496 664-944/? E/﹕ error : 2, No such file or directory
07-11 09:02:35.064 16307-16323/? E/chromium_android_linker﹕ UseSharedRelro: Could not use shared RELRO for libchrome.so: No pages were swapped into RELRO ashmem
07-11 09:02:35.131 16307-16323/? E/﹕ appName=com.android.chrome:privileged_process0, acAppName=/system/bin/surfaceflinger
07-11 09:02:35.131 16307-16323/? E/﹕ 0
07-11 09:02:35.131 16307-16323/? E/﹕ appName=com.android.chrome:privileged_process0, acAppName=/system/bin/surfaceflinger
07-11 09:02:35.131 16307-16323/? E/﹕ 0
07-11 09:02:35.229 1003-1003/? E/NetworkScheduler.SchedulerReceiver﹕ Invalid parameter app
07-11 09:02:35.229 1003-1003/? E/NetworkScheduler.SchedulerReceiver﹕ Invalid package name : Perhaps you didn't include a PendingIntent in the extras?
07-11 09:02:35.300 16307-16323/? E/﹕ appName=com.android.chrome:privileged_process0, acAppName=/system/bin/surfaceflinger
07-11 09:02:35.301 16307-16323/? E/﹕ 0
07-11 09:02:35.301 16307-16323/? E/﹕ appName=com.android.chrome:privileged_process0, acAppName=/system/bin/surfaceflinger
07-11 09:02:35.301 16307-16323/? E/﹕ 0
07-11 09:02:35.325 16307-16323/? E/﹕ appName=com.android.chrome:privileged_process0, acAppName=/system/bin/surfaceflinger
07-11 09:02:35.325 16307-16323/? E/﹕ 0
07-11 09:02:35.325 16307-16323/? E/﹕ appName=com.android.chrome:privileged_process0, acAppName=/system/bin/surfaceflinger
07-11 09:02:35.325 16307-16323/? E/﹕ 0
07-11 09:02:36.016 16370-16370/? E/﹕ appName=com.google.android.apps.magazines, acAppName=/system/bin/surfaceflinger
07-11 09:02:36.016 16370-16370/? E/﹕ 0
07-11 09:02:36.016 16370-16370/? E/﹕ appName=com.google.android.apps.magazines, acAppName=/system/bin/surfaceflinger
07-11 09:02:36.016 16370-16370/? E/﹕ 0
07-11 09:02:36.791 16422-16440/? E/NativeCrypto﹕ ssl=0x607e57a8 cert_verify_callback x509_store_ctx=0x610ad8c0 arg=0x0
07-11 09:02:36.791 16422-16440/? E/NativeCrypto﹕ ssl=0x607e57a8 cert_verify_callback calling verifyCertificateChain authMethod=ECDHE_RSA
07-11 09:02:36.813 16470-16470/? E/dalvikvm﹕ Could not find class 'com.amazon.device.messaging.ADM', referenced from method com.appboy.Appboy.<init>
07-11 09:02:37.291 664-1595/? E/ActivityManager﹕ Service ServiceRecord{42d9f2b0 u0 com.google.android.youtube/com.google.android.apps.youtube.app.offline.transfer.OfflineTransferService} in process ProcessRecord{42c8a980 15786:com.google.android.youtube/u0a91} not same as in map: null
07-11 09:02:37.348 16470-16542/? E/NativeCrypto﹕ ssl=0x62875cc0 cert_verify_callback x509_store_ctx=0x6274a940 arg=0x0
07-11 09:02:37.348 16470-16542/? E/NativeCrypto﹕ ssl=0x62875cc0 cert_verify_callback calling verifyCertificateChain authMethod=ECDHE_RSA
07-11 09:02:37.453 1003-1003/? E/AuthorizationBluetoothService﹕ Proximity feature is not enabled.
07-11 09:02:37.456 16470-16532/? E/NativeCrypto﹕ ssl=0x629948f8 cert_verify_callback x509_store_ctx=0x623a1940 arg=0x0
07-11 09:02:37.456 16470-16532/? E/NativeCrypto﹕ ssl=0x629948f8 cert_verify_callback calling verifyCertificateChain authMethod=RSA
07-11 09:02:37.482 16470-16529/? E/NativeCrypto﹕ ssl=0x628abbb0 cert_verify_callback x509_store_ctx=0x6203e940 arg=0x0
07-11 09:02:37.483 16470-16529/? E/NativeCrypto﹕ ssl=0x628abbb0 cert_verify_callback calling verifyCertificateChain authMethod=RSA
07-11 09:02:37.526 16294-16294/? E/dalvikvm﹕ Could not find class 'android.telecom.TelecomManager', referenced from method com.google.android.gms.wearable.service.y.a
07-11 09:02:37.603 858-2870/? E/MDM﹕ [67] b.run: Couldn't connect to Google API client: ConnectionResult{statusCode=API_UNAVAILABLE, resolution=null}
07-11 09:02:38.568 16572-16572/? E/dalvikvm﹕ Could not find class 'android.app.Notification$Action$Builder', referenced from method b.a
07-11 09:02:38.584 16572-16572/? E/dalvikvm﹕ Could not find class 'al', referenced from method b.a
07-11 09:02:38.586 16572-16572/? E/dalvikvm﹕ Could not find class 'an', referenced from method b.a
07-11 09:02:38.592 16572-16572/? E/dalvikvm﹕ Could not find class 'android.app.RemoteInput[]', referenced from method b.a
07-11 09:02:38.871 16572-16572/? E/WifiManager﹕ mWifiServiceMessenger == null
07-11 09:02:39.316 16572-16572/? E/WifiManager﹕ mWifiServiceMessenger == null
07-11 09:02:42.352 16422-16422/? E/ActivityThread﹕ Failed to find provider info for com.facebook.katana.provider.AttributionIdProvider
07-11 09:02:42.366 16422-16663/? E/ActivityThread﹕ Failed to find provider info for com.facebook.katana.provider.AttributionIdProvider
07-11 09:02:42.546 16422-16439/? E/NativeCrypto﹕ ssl=0x607e3ed0 cert_verify_callback x509_store_ctx=0x60f6f940 arg=0x0
07-11 09:02:42.547 16422-16439/? E/NativeCrypto﹕ ssl=0x607e3ed0 cert_verify_callback calling verifyCertificateChain authMethod=ECDHE_ECDSA
07-11 09:02:58.108 143-17153/? E/AudioMTKHardware﹕ setCommonParameters() still have param.size() = 1, remain param = "screen_state=off"
07-11 09:02:59.036 973-1156/? E/ActivityThread﹕ Failed to find provider info for com.gionee.account
07-11 09:03:00.337 664-664/? E/RemoteViews﹕ ANR Warning,RemoteViews can only be used once ,if not ,it may cause ANR in hosts such as Laucher,SystemUI. keys for search <ANR Exception MSG History>
07-11 09:03:03.056 735-735/? E/KeyguardHostView﹕ KeyguardHostView()
我已在联系人和联系人阅读清单文件中添加了所有权限。
请告诉我需要做些什么改变才能让它发挥作用。
谢谢。
答案 0 :(得分:0)
您正在获取NPE(又名NullPointerException),因为您手动创建了一个不好的IntentService:saveContacts contacts = new saveContacts("");
这是使用IntentServices的tutorial。
或者在您的情况下,将代码移出onHandleIntent
并将其直接放入doInBackground
可能是有意义的。