0

can anyone tell me, what is the problem in here? I have been trying the whole day. I am new in android and i am not good in reading the logcat either, thus, I really hope anyone could help me out. the application force closes all the time.

private ProgressDialog pDialog;

    // Creating JSON Parser object
    JSONParser jParser = new JSONParser();

    ArrayList<HashMap<String, String>> eventsList;

    // url to get all products list
    private static String url_all_products = "http://xx.php";

    // JSON Node names
    private static final String TAG_SUCCESS = "success";
    private static final String TAG_CUSTOMER = "customer";
    private static final String TAG_PID = "pid";
    private static final String TAG_FIRSTNAME = "firstname";
    private static final String TAG_LASTNAME = "lastname";
    private static final String TAG_ADDRESS = "address";
    private static final String TAG_TOTALCOST = "totalCost";

    // products JSONArray
    JSONArray events = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_customer_transaction);

        // Hashmap for ListView
        eventsList = new ArrayList<HashMap<String, String>>();

        // Loading products in Background Thread
        new LoadAllProducts().execute();

    }


    /**
     * Background Async Task to Load all product by making HTTP Request
     * */
    class LoadAllProducts extends AsyncTask<String, String, String> {

        /**
         * Before starting background thread Show Progress Dialog
         * */
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(CustomerTransaction.this);
            pDialog.setMessage("Loading. Please wait...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();
        }

        /**
         * getting All products from url
         * */
        protected String doInBackground(String... args) {
            // Building Parameters
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            // getting JSON string from URL
            JSONObject json = jParser.makeHttpRequest(url_all_products, "GET", params);

            // Check your log cat for JSON reponse
            Log.d("All Products: ", json.toString());

            try {
                // Checking for SUCCESS TAG
                int success = json.getInt(TAG_SUCCESS);

                if (success == 1) {
                    // products found
                    // Getting Array of Products
                    events = json.getJSONArray(TAG_CUSTOMER);

                    // looping through All Products
                    for (int i = 0; i < events.length(); i++) {
                        JSONObject c = events.getJSONObject(i);

                        // Storing each json item in variable
                        String pid = c.getString(TAG_PID);
                        String firstname = c.getString(TAG_FIRSTNAME);
                        String lastname = "Time :" +c.getString(TAG_LASTNAME);
                        String address = "Date :" +c.getString(TAG_ADDRESS);
                        String totalCost = "Venue :" +c.getString(TAG_TOTALCOST);


                        // creating new HashMap
                        HashMap<String, String> map = new HashMap<String, String>();

                        // adding each child node to HashMap key => value
                        map.put(TAG_PID, pid);
                        map.put(TAG_FIRSTNAME, firstname);
                        map.put(TAG_LASTNAME, lastname);
                        map.put(TAG_ADDRESS, address);
                        map.put(TAG_TOTALCOST, totalCost);
                        // adding HashList to ArrayList
                        eventsList.add(map);
                    }
                } 

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

            return null;
        }

        /**
         * After completing background task Dismiss the progress dialog
         * **/
        protected void onPostExecute(String file_url) {
            // dismiss the dialog after getting all products
            pDialog.dismiss();
            // updating UI from Background Thread
            runOnUiThread(new Runnable() {
                public void run() {
                    /**
                     * Updating parsed JSON data into ListView
                     * */
                    ListAdapter adapter = new SimpleAdapter(
                            CustomerTransaction.this, eventsList,
                            R.layout.list_item4, new String[] { TAG_PID,TAG_FIRSTNAME, TAG_LASTNAME, TAG_ADDRESS, TAG_TOTALCOST},
                            new int[] { R.id.pid, R.id.firstname, R.id.lastname, R.id.address, R.id.totalCost });
                    // updating listview
                    setListAdapter(adapter);
                }
            });

        }

    }

the logcat:

04-16 18:34:22.706: E/WindowManager(1019): Activity com.spyraa.bookstore.CustomerTransaction has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{40d2c1e8 V.E..... R.....ID 0,0-304,96} that was originally added here
 04-16 18:34:22.706: E/WindowManager(1019): android.view.WindowLeaked: Activity com.spyraa.bookstore.CustomerTransaction has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{40d2c1e8 V.E..... R.....ID 0,0-304,96} that was originally added here
04-16 18:34:22.706: E/WindowManager(1019):  at android.view.ViewRootImpl.<init>(ViewRootImpl.java:354)
04-16 18:34:22.706: E/WindowManager(1019):  at  android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:216)
04-16 18:34:22.706: E/WindowManager(1019):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
04-16 18:34:22.706: E/WindowManager(1019):  at android.app.Dialog.show(Dialog.java:281)
 04-16 18:34:22.706: E/WindowManager(1019):     at com.spyraa.bookstore.CustomerTransaction$LoadAllProducts.onPreExecute(CustomerTransaction.java:84)
04-16 18:34:22.706: E/WindowManager(1019):  at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
 04-16 18:34:22.706: E/WindowManager(1019):     at android.os.AsyncTask.execute(AsyncTask.java:534)
04-16 18:34:22.706: E/WindowManager(1019):  at com.spyraa.bookstore.CustomerTransaction.onCreate(CustomerTransaction.java:61)
04-16 18:34:22.706: E/WindowManager(1019):  at android.app.Activity.performCreate(Activity.java:5104)
04-16 18:34:22.706: E/WindowManager(1019):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
04-16 18:34:22.706: E/WindowManager(1019):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
04-16 18:34:22.706: E/WindowManager(1019):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
04-16 18:34:22.706: E/WindowManager(1019):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
04-16 18:34:22.706: E/WindowManager(1019):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
04-16 18:34:22.706: E/WindowManager(1019):  at android.os.Handler.dispatchMessage(Handler.java:99)
04-16 18:34:22.706: E/WindowManager(1019):  at android.os.Looper.loop(Looper.java:137)
04-16 18:34:22.706: E/WindowManager(1019):  at android.app.ActivityThread.main(ActivityThread.java:5039)
04-16 18:34:22.706: E/WindowManager(1019):  at java.lang.reflect.Method.invokeNative(Native Method)
04-16 18:34:22.706: E/WindowManager(1019):  at java.lang.reflect.Method.invoke(Method.java:511)
04-16 18:34:22.706: E/WindowManager(1019):  at     com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-16 18:34:22.706: E/WindowManager(1019):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-16 18:34:22.706: E/WindowManager(1019):  at dalvik.system.NativeStart.main(Native Method)

thank you so much.

thegrinner
  • 11,546
  • 5
  • 41
  • 64
A.K.C.F.L
  • 45
  • 3
  • 11

2 Answers2

0

use the request like that

HttpPost httpost = new HttpPost(url_all_products);

List <NameValuePair> nvps = new ArrayList <NameValuePair>();
nvps.add(new BasicNameValuePair("test1","test1" ));
nvps.add(new BasicNameValuePair("test2", "test2" ));

        httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));

response = getResponse(httpost);
Sunil Kumar
  • 7,086
  • 4
  • 32
  • 50
0

I see several issues. The first I think is causing your problem. Move your progressbar initialization out of onPreExecute() to inside the class declaration

 class LoadAllProducts extends AsyncTask<String, String, String> {

  pDialog = new ProgressDialog(CustomerTransaction.this);  // move to here so you can cancel in onPostExecute()
    /**
     * Before starting background thread Show Progress Dialog
     * */
    @Override
    protected void onPreExecute() {
        super.onPreExecute();

You don't need runOnUiThread in onPostExecute() since this method runs on the UI thread. You are taking a parameter in onPostExecute() but don't appear to be returning anything from doInBackground(). There may be more issues but I would try these things first and see what you get

Knowing how to use the logcat is very important. Here it tells you that a window has leaked which I believe is caused by where you initialize your progressbar. Usually you will see a line that says something like "Caused by Null Pointer Exception" or other exception. When you find this line, the first line that references your project

 at com.spyraa.bookstore.CustomerTransaction$LoadAllProducts.onPreExecute(CustomerTransaction.java:84)

for example, will tell you that line 84 is where your error occurred. This is usually a good place to start looking. Hope this helps

codeMagic
  • 44,549
  • 13
  • 77
  • 93