android两个asynctask运行同一时间运行时异常

时间:2014-10-02 10:44:41

标签: android android-asynctask

我正在工作asynctask.i有两个asynctas一个活动。在第一个asynctask我用来解析json并在listview中显示它,第二个 - 按位置下载图像。

这是我的代码

public class MainmoviesList extends Fragment implements
    AdapterView.OnItemSelectedListener {
    public final static String TAG = MainmoviesList.class.getSimpleName();
    String imageurl = "**********";
    public static List<ServerItems> arrayOfList;
    private ProgressDialog pDialog2;
    public static Gallery main_listview;
    private AzercellMainPageAdapter objAdapter;

    private RelativeLayout mSwitcher;
    private CustomerStatistic cs;
    private String Eng_Url = "*************"; // eng
    public static int mPosition;

    public static MainmoviesList newInstance() {
        return new MainmoviesList();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.main_movies_list, container,
                false);

        mSwitcher = (RelativeLayout) rootView.findViewById(R.id.rootNode);
        main_listview = (Gallery) rootView
                .findViewById(R.id.horizontallistview);
        main_listview.setOnItemSelectedListener(this);
        main_listview.setSelection(mPosition);

        arrayOfList = new ArrayList<ServerItems>();
        cs = new CustomerStatistic();

        cs.execute(Eng_Url);

        return rootView;

    }

    private class CustomerStatistic extends AsyncTask<String, Void, String> {

        ProgressDialog pDialog;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();

            pDialog = new ProgressDialog(getActivity());

            pDialog.setCancelable(false);
            pDialog.show();
            pDialog.setContentView(R.layout.custom_progressdialog);
        }

        @Override
        protected String doInBackground(String... params) {

            return Utils.getJSONString(params[0]);
        }

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);

            try {
                Globalclass.MovieServer = result;
                JSONArray mainJson = new JSONArray(result);

                for (int i = 0; i < mainJson.length(); i++) {
                    JSONObject objJson = mainJson.getJSONObject(i);

                    JSONObject cinema = objJson.getJSONObject("Cinemas");
                    JSONArray mainJson1 = cinema.getJSONArray("Cinemaname");

                    for (int j = 0; j < mainJson1.length(); j++) {
                        JSONObject objJson1 = mainJson1.getJSONObject(j);
                        Log.e("cinema name", objJson1.getString("cinemaName"));

                    }

                    ServerItems objItem = new ServerItems();

                    objItem.setImage(imageurl + objJson.getString("ipone_4"));
                    objItem.setTitle(objJson.getString("title"));
                    objItem.setYoutube(objJson.getString("youtube"));
                    objItem.setWritten(objJson.getString("written"));

                    objItem.setStars(objJson.getString("stars"));
                    objItem.setBlurimage(imageurl
                            + objJson.getString("ipone_4_blur"));



                    arrayOfList.add(objItem);

                }

            } catch (JSONException e) {
                e.printStackTrace();
            }

            if (pDialog != null) {
                pDialog.dismiss();

                pDialog = null;
            }
            setAdapterToListview();

        }
    }

    public void setAdapterToListview() {
        // objAdapter = new AzercellMainPageAdapter(getApplicationContext(),
        // R.layout.azercell_main_page_adapter, arrayOfList);
        objAdapter = new AzercellMainPageAdapter(getActivity(),
                R.layout.azercell_main_page_adapter, arrayOfList);

        main_listview.setAdapter(objAdapter);
    }

    public void loadimagePosition(int pos) {

    }

    private void SendFlagIdToServer1(final int position) {
        class HttpGetAsyncTask extends AsyncTask<String, Void, String> {

            @Override
            protected void onPreExecute() {
                super.onPreExecute();

                pDialog2 = new ProgressDialog(getActivity());

                pDialog2.setCancelable(false);
                pDialog2.show();
                pDialog2.setContentView(R.layout.custom_progressdialog);
            }

            @Override
            protected String doInBackground(String... params) {

                URL url;
                try {
                    url = new URL(arrayOfList.get(position).getBlurimage());
                    Bitmap bmp = BitmapFactory.decodeStream(url
                            .openConnection().getInputStream());

                    Drawable drawable = new BitmapDrawable(bmp);

                    mSwitcher.setBackgroundDrawable(drawable);

                } catch (MalformedURLException e) {

                    e.printStackTrace();
                } catch (IOException e) {

                    e.printStackTrace();
                }

                return null;
            }

            @Override
            protected void onPostExecute(String result) {
                super.onPostExecute(result);

                if (pDialog2 != null) {
                    pDialog2.dismiss();

                    pDialog2 = null;
                }

            }
        }

        HttpGetAsyncTask httpGetAsyncTask = new HttpGetAsyncTask();

        httpGetAsyncTask.execute();

    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position,
            long id) {
        mPosition = position;
        SendFlagIdToServer1(position);

    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {
        // TODO Auto-generated method stub

    }
}

我有运行时异常。两个asynctas类同时运行? 可以同时运行同一个asynctass一个活动吗? 如果有人知道解决方案,请帮助我 感谢

0 个答案:

没有答案