我想從foor循環調用異步任務我該怎麼做? 我已經做到了這一點安卓異步任務for循環
for (int i = 0; i < listimage.size(); i++) {
GlobalVariable.set_URL(listimage.get(i) + "");
System.out.println("from fav " + GlobalVariable.get_URL() + "");
try {
obj = new JSONObject(GlobalVariable.get_URL());
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
GetContacts gc = new GetContacts();
gc.execute();
}
我的logcat:
04-08 14:27:33.894: E/AndroidRuntime(2526): FATAL EXCEPTION: AsyncTask #5
04-08 14:27:33.894: E/AndroidRuntime(2526): java.lang.RuntimeException: An error occured while executing doInBackground()
04-08 14:27:33.894: E/AndroidRuntime(2526): at android.os.AsyncTask$3.done(AsyncTask.java:299)
04-08 14:27:33.894: E/AndroidRuntime(2526): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
04-08 14:27:33.894: E/AndroidRuntime(2526): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
04-08 14:27:33.894: E/AndroidRuntime(2526): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
04-08 14:27:33.894: E/AndroidRuntime(2526): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
04-08 14:27:33.894: E/AndroidRuntime(2526): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
04-08 14:27:33.894: E/AndroidRuntime(2526): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
04-08 14:27:33.894: E/AndroidRuntime(2526): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
04-08 14:27:33.894: E/AndroidRuntime(2526): at java.lang.Thread.run(Thread.java:856)
04-08 14:27:33.894: E/AndroidRuntime(2526): Caused by: java.lang.NullPointerException
04-08 14:27:33.894: E/AndroidRuntime(2526): at com.leeway.hdwallpaper.favourite_Activity$GetContacts.doInBackground(favourite_Activity.java:182)
04-08 14:27:33.894: E/AndroidRuntime(2526): at com.leeway.hdwallpaper.favourite_Activity$GetContacts.doInBackground(favourite_Activity.java:1)
04-08 14:27:33.894: E/AndroidRuntime(2526): at android.os.AsyncTask$2.call(AsyncTask.java:287)
04-08 14:27:33.894: E/AndroidRuntime(2526): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
04-08 14:27:33.894: E/AndroidRuntime(2526): ... 5 more
04-08 14:27:34.305: E/WindowManager(2526): Activity com.leeway.hdwallpaper.MainActivity has leaked window [email protected] that was originally added here
04-08 14:27:34.305: E/WindowManager(2526): android.view.WindowLeaked: Activity com.leeway.hdwallpaper.MainActivity has leaked window [email protected] that was originally added here
04-08 14:27:34.305: E/WindowManager(2526): at android.view.ViewRootImpl.<init>(ViewRootImpl.java:374)
04-08 14:27:34.305: E/WindowManager(2526): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:297)
04-08 14:27:34.305: E/WindowManager(2526): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:229)
04-08 14:27:34.305: E/WindowManager(2526): at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:154)
04-08 14:27:34.305: E/WindowManager(2526): at android.view.Window$LocalWindowManager.addView(Window.java:547)
04-08 14:27:34.305: E/WindowManager(2526): at android.app.Dialog.show(Dialog.java:285)
04-08 14:27:34.305: E/WindowManager(2526): at com.leeway.hdwallpaper.favourite_Activity$GetContacts.onPreExecute(favourite_Activity.java:161)
04-08 14:27:34.305: E/WindowManager(2526): at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
04-08 14:27:34.305: E/WindowManager(2526): at android.os.AsyncTask.execute(AsyncTask.java:534)
04-08 14:27:34.305: E/WindowManager(2526): at com.leeway.hdwallpaper.favourite_Activity.onCreateView(favourite_Activity.java:108)
04-08 14:27:34.305: E/WindowManager(2526): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
04-08 14:27:34.305: E/WindowManager(2526): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
04-08 14:27:34.305: E/WindowManager(2526): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
04-08 14:27:34.305: E/WindowManager(2526): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
04-08 14:27:34.305: E/WindowManager(2526): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
04-08 14:27:34.305: E/WindowManager(2526): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:440)
04-08 14:27:34.305: E/WindowManager(2526): at android.os.Handler.handleCallback(Handler.java:643)
04-08 14:27:34.305: E/WindowManager(2526): at android.os.Handler.dispatchMessage(Handler.java:92)
04-08 14:27:34.305: E/WindowManager(2526): at android.os.Looper.loop(Looper.java:137)
04-08 14:27:34.305: E/WindowManager(2526): at android.app.ActivityThread.main(ActivityThread.java:4803)
04-08 14:27:34.305: E/WindowManager(2526): at java.lang.reflect.Method.invokeNative(Native Method)
04-08 14:27:34.305: E/WindowManager(2526): at java.lang.reflect.Method.invoke(Method.java:511)
04-08 14:27:34.305: E/WindowManager(2526): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
04-08 14:27:34.305: E/WindowManager(2526): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
04-08 14:27:34.305: E/WindowManager(2526): at dalvik.system.NativeStart.main(Native Method)
這是我getcontact.doinbackground
protected Void doInBackground(Void... params) {
sample = new ArrayList<String>();
contactList = new ArrayList<HashMap<String, String>>();
gv = (GridView) getActivity().findViewById(R.id.gridView1);
ServiceHandler sh = new ServiceHandler();
String jsonStr = sh.makeServiceCall(obj.toString(),
ServiceHandler.GET);
System.out.println("this is from do in.."
+ GlobalVariable.get_URL() + "");
Log.d("Response: ", "> " + jsonStr);
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
// Getting JSON Array node
contacts = jsonObj.getJSONArray(TAG_WALLPAPER);
// looping through All Contacts
for (int i = 0; i < contacts.length(); i++) {
JSONObject c = contacts.getJSONObject(i);
String id = c.getString(TAG_ID);
// Phone node is JSON Object
JSONObject phone = c.getJSONObject(TAG_IMAGE);
String thumb = phone.getString(TAG_THUMB);
String android = phone.getString(TAG_IMAGE_ANDROID);
// img_url.add(thumb);
// grdadpter ga = new grdadpter(getActivity(), sample);
// gv.setAdapter(ga);
// String office = phone.getString(TAG_IMAGE_OFFICE);
// tmp hashmap for single contact
HashMap<String, String> contact = new HashMap<String, String>();
// adding each child node to HashMap key => value
contact.put(TAG_ID, id);
img_ID.add(id);
// adding contact to contact list
contactList.add(contact);
// sample.add(thumb);
publishProgress(thumb);
android_img.add(android);
System.out.println("this is from do in ng.."
+ sample.size());
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Log.e("ServiceHandler", "Couldn't get any data from the url");
}
return null;
}
可以請您展示'GetContacts'類嗎? – nikis
看到我更新的問題 – Android
'favourite_Activity.java:182'上有什麼? – nikis