在异步类运行时,android无法访问应用程序

时间:2014-08-21 05:10:10

标签: android android-asynctask

我在我的应用程序中执行异步类在该特定时间段内(同时在后台运行类进程)我无法从我的应用程序访问任何内容。我触摸它所做的任何工作,并在Async类进程完成后正常运行。    所以,我想要解决方案,如果我想在应用程序中进行更改,那时更新过程将在后台运行,我也可以访问此应用程序。并提出申请的其他要求。所以请任何解决我的问题的人请给我解决这个问题。

我的代码在这里给出

class GetUserDetail extends AsyncTask<String, String, String> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(wishlist.this);
            pDialog.setMessage("Loading details. Please wait...");
            pDialog.setIndeterminate(false);
            pDialog.show();

        }

        protected String doInBackground(String... params) {
            runOnUiThread(new Runnable() {
                public void run() {
                    try {
                        List<NameValuePair> params = new ArrayList<NameValuePair>();
                        params.add(new BasicNameValuePair("name", "xyz"));
                        JSONObject json = jsonpd.makeHttpRequest(url, "POST",
                                params);
                        Log.d("Single user Details", json.toString());
                        // JSONObject mainJson = new
                        // JSONObject(json.toString());
                        JSONArray json_contents = json.getJSONArray("Success");

                        for (int i = 0; i < json_contents.length(); i++) {
                            JSONObject c = json_contents.getJSONObject(i);
                        //  tv.setVisibility(View.GONE);
                            HashMap<String, String> map = new HashMap<String, String>();
                            map.put(TAG_name, c.getString(TAG_name));
                            map.put(TAG_image, c.getString(TAG_image));
                            map.put(TAG_product_id, c.getString(TAG_product_id));
                            map.put(TAG_des, c.getString(TAG_des));
                            map.put(TAG_price, c.getString(TAG_price));

                            arraylist.add(map);

                        }

                    } catch (Exception e) {

                        tv.setVisibility(View.VISIBLE);
                    }

                }
            });

            return null;
        }

        protected void onPostExecute(String file_url) {
            // Pass the results into ListViewAdapter.java
            adapters = new WishListViewAdapter(wishlist.this, arraylist);
            // Set the adapter to the ListView
            wishlist.setAdapter(adapters);

            // Close the progressdialog
            pDialog.dismiss();

        }
    }

现在看到这个适配器类

public class WishListViewAdapter extends BaseAdapter {

    String addtocartz="spiurl";
    TextView pname, pdes, pprice, pid;
    Context context;
    LayoutInflater inflater;
    ArrayList<HashMap<String, String>> data;
    ImageLoader imageLoader;
    String PID, PNAME;
    DecimalFormat dec = new DecimalFormat("#.##");
    JSONParser jsonpd = new JSONParser();
    UserSessionManager session;
    ProgressDialog pDialog;
    HashMap<String, String> resultp = new HashMap<String, String>();

    public WishListViewAdapter(Context context,
            ArrayList<HashMap<String, String>> arraylist) {
        this.context = context;
        data = arraylist;
        imageLoader = new ImageLoader(context);

    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    public View getView(final int position, View convertView, ViewGroup parent) {
        // Declare Variables

        ImageView pimage;
        Button addtocart;
        dec.setMinimumFractionDigits(2);
        inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View itemView = inflater.inflate(R.layout.wishlist_row, parent, false);
        resultp = data.get(position);
        pname = (TextView) itemView.findViewById(R.id.wpname);
        pdes = (TextView) itemView.findViewById(R.id.wpdiscription);
        pprice = (TextView) itemView.findViewById(R.id.wpprice);
        addtocart = (Button) itemView.findViewById(R.id.wAdvance_Search_Button);
        pimage = (ImageView) itemView.findViewById(R.id.wpimage);

        if (resultp.get(Search.TAG_price) != ("null")) {
            pprice.setText("$"
                    + dec.format(Double.valueOf(resultp.get(wishlist.TAG_price))));

        } else {
            pprice.setText("$" + resultp.get(wishlist.TAG_price));

        }   
        pname.setText(resultp.get(wishlist.TAG_name));
        pdes.setText(resultp.get(wishlist.TAG_des));
        imageLoader.DisplayImage(resultp.get(wishlist.TAG_image), pimage);
        addtocart.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                session = new UserSessionManager(context);
                if (session.checkLogin())
                    finish();
                else{

                resultp = data.get(position);


                HashMap<String, String> users = session.getUserDetails();

                List<NameValuePair> params = new ArrayList<NameValuePair>();
                params.add(new BasicNameValuePair("customer_id", users
                        .get(UserSessionManager.KEY_CUSTOMER_ID)));
                params.add(new BasicNameValuePair("product_id", resultp
                        .get(wishlist.TAG_product_id)));
                params.add(new BasicNameValuePair("qty", "1"));

                JSONObject json = jsonpd.makeHttpRequest(addtocartz, "POST",
                        params);

                pDialog.dismiss();
                Toast.makeText(context, "Product successfully added to cart",
                        Toast.LENGTH_LONG).show();
                }

            }

            private void finish() {
            }
        });

        return itemView;
    }

    public void runOnUiThread(Runnable runnable) {
        // TODO Auto-generated method stub

    }

    }

现在当我点击按钮时发生的事情应用程序将停止,就像它悬挂但实际上在那个时候在背景中

HashMap<String, String> users = session.getUserDetails();

                List<NameValuePair> params = new ArrayList<NameValuePair>();
                params.add(new BasicNameValuePair("customer_id", users
                        .get(UserSessionManager.KEY_CUSTOMER_ID)));
                params.add(new BasicNameValuePair("product_id", resultp
                        .get(wishlist.TAG_product_id)));
                params.add(new BasicNameValuePair("qty", "1"));

                JSONObject json = jsonpd.makeHttpRequest(addtocartz, "POST",
                        params);

                pDialog.dismiss();
                Toast.makeText(context, "Product successfully added to cart",
                        Toast.LENGTH_LONG).show();

上面的代码正在运行,此过程执行后我可以再次正常访问此应用程序。

希望现在可以清楚我的问题。

1 个答案:

答案 0 :(得分:0)

runOnUiThread

中删除doInBackground
protected String doInBackground(String... params) {
                try {
                    List<NameValuePair> params = new ArrayList<NameValuePair>();
                    params.add(new BasicNameValuePair("name", "xyz"));
                    JSONObject json = jsonpd.makeHttpRequest(url, "POST",
                            params);
                    Log.d("Single user Details", json.toString());
                    // JSONObject mainJson = new
                    // JSONObject(json.toString());
                    JSONArray json_contents = json.getJSONArray("Success");

                    for (int i = 0; i < json_contents.length(); i++) {
                        JSONObject c = json_contents.getJSONObject(i);
                    //  tv.setVisibility(View.GONE);
                        HashMap<String, String> map = new HashMap<String, String>();
                        map.put(TAG_name, c.getString(TAG_name));
                        map.put(TAG_image, c.getString(TAG_image));
                        map.put(TAG_product_id, c.getString(TAG_product_id));
                        map.put(TAG_des, c.getString(TAG_des));
                        map.put(TAG_price, c.getString(TAG_price));

                        arraylist.add(map);

                    }

                } catch (Exception e) {

                    tv.setVisibility(View.VISIBLE);

          }       

        return null;
    }