for循环中的android异步任务

时间:2014-04-08 09:01:11

标签: android for-loop android-asynctask

我想从foor循环调用异步任务我怎么能这样做?  我做了这个

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 com.android.internal.policy.impl.PhoneWindow$DecorView@42642538 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 com.android.internal.policy.impl.PhoneWindow$DecorView@42642538 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;

}

4 个答案:

答案 0 :(得分:1)

for循环中的

AsynTask?您的for循环不会等待AsyncTask完成。 在给定时间内,最多只允许5 AsynTask

答案 1 :(得分:0)

试试这个..

我想你没有在img_ID ArrayList内部启动doInBackground img_ID = new ArrayList<String>();

GridView

修改

同样地,您无法在doInBackgroundActivity Fragment中删除{{1}}或{{1}}

答案 2 :(得分:0)

因为您在异步通话中没有返回任何内容而不是

new GetContacts().execute();

或者您需要调整异步调用以返回联系人??

答案 3 :(得分:0)

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();

        }

change to

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[i] = new GetContacts();
            gc[i].execute();

        }