0

I am trying to load the data from mysql database into a listview in android.The progress dialog appears then the app crashes.The error what i get is activity has leaked window.Please tell me What is the problem which is causing this error.This is the java activity file

    public class CompScience extends ListActivity {
// Progress Dialog
        private ProgressDialog pDialog;

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

        ArrayList<HashMap<String, String>> booksList;

        // url to get all products list
        private static String url= "http://10.0.2.2/books/get_all_books.php";

        // JSON Node names
        private static final String TAG_SUCCESS = "success";
        private static final String TAG_BOOKS = "computerscience";
        private static final String TAG_TITLE = "title";
        private static final String TAG_BID = "bid";
        private static final String TAG_AUTHOR = "author";

        // products JSONArray
        JSONArray books = null;

       @Override
       protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_comp_science);

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

    // Loading products in Background Thread
                new LoadAllProducts().execute();
    // Get listview
                ListView lv = getListView();
                lv.setOnItemClickListener(new OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view,
                            int position, long id) {
                        //String description = ((TextView) view).getText().toString();
                        Intent i = new Intent(CompScience.this,List1.class);
                    //  i.putExtra("description", description);


                        startActivity(i);
                    }

                });
            }

        class LoadAllProducts extends AsyncTask<String, String, String> {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(CompScience.this);
        pDialog.setMessage("Loading...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.show();
    }
    protected String doInBackground(String... args) {
        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        // getting JSON string from URL
        JSONObject json = jParser.makeHttpRequest(url, "GET", params);
        // Check your log cat for JSON reponse
        Log.d("All Books: ", json.toString());

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

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

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

                // Storing each json item in variable
                String bid = c.getString(TAG_BID);
                String title = c.getString(TAG_TITLE);
                String author = c.getString(TAG_AUTHOR);

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

                // adding each child node to HashMap key => value
                map.put(TAG_BID, bid);
                map.put(TAG_TITLE, title);
                map.put(TAG_AUTHOR, author);

                // adding HashList to ArrayList
                booksList.add(map);
            }
        } else {
            Log.e(TAG_BOOKS, "No books found");
         }
          } 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(
                    CompScience.this,booksList,
                    R.layout.list_item, new String[] { TAG_BID,
                            TAG_TITLE,TAG_AUTHOR},
                    new int[] { R.id.bid, R.id.title,R.id.author });
            // updating listview
            setListAdapter(adapter);
        }
    });

      }

Here is my logcat file

    04-07 06:13:09.924: E/WindowManager(1617): Activity com.example.dashboard.CompScience has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{41750a58 V.E..... R.....ID 0,0-228,72} that was originally added here
    04-07 06:13:09.924: E/WindowManager(1617): android.view.WindowLeaked: Activity com.example.dashboard.CompScience has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{41750a58 V.E..... R.....ID 0,0-228,72} that was originally added here
    04-07 06:13:09.924: E/WindowManager(1617):  at android.view.ViewRootImpl.<init>(ViewRootImpl.java:345)
    04-07 06:13:09.924: E/WindowManager(1617):  at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:239)
    04-07 06:13:09.924: E/WindowManager(1617):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
    04-07 06:13:09.924: E/WindowManager(1617):  at android.app.Dialog.show(Dialog.java:281)
    04-07 06:13:09.924: E/WindowManager(1617):  at com.example.dashboard.CompScience$LoadAllProducts.onPreExecute(CompScience.java:85)
    04-07 06:13:09.924: E/WindowManager(1617):  at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
    04-07 06:13:09.924: E/WindowManager(1617):  at android.os.AsyncTask.execute(AsyncTask.java:534)
    04-07 06:13:09.924: E/WindowManager(1617):  at com.example.dashboard.CompScience.onCreate(CompScience.java:59)
    04-07 06:13:09.924: E/WindowManager(1617):  at android.app.Activity.performCreate(Activity.java:5133)
    04-07 06:13:09.924: E/WindowManager(1617):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
    04-07 06:13:09.924: E/WindowManager(1617):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
    04-07 06:13:09.924: E/WindowManager(1617):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
    04-07 06:13:09.924: E/WindowManager(1617):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
    04-07 06:13:09.924: E/WindowManager(1617):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
    04-07 06:13:09.924: E/WindowManager(1617):  at android.os.Handler.dispatchMessage(Handler.java:99)
    04-07 06:13:09.924: E/WindowManager(1617):  at android.os.Looper.loop(Looper.java:137)
    04-07 06:13:09.924: E/WindowManager(1617):  at android.app.ActivityThread.main(ActivityThread.java:5103)
    04-07 06:13:09.924: E/WindowManager(1617):  at java.lang.reflect.Method.invokeNative(Native Method)
    04-07 06:13:09.924: E/WindowManager(1617):  at java.lang.reflect.Method.invoke(Method.java:525)
    04-07 06:13:09.924: E/WindowManager(1617):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
    04-07 06:13:09.924: E/WindowManager(1617):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
    04-07 06:13:09.924: E/WindowManager(1617):  at dalvik.system.NativeStart.main(Native Method)

Please help

sheetal
  • 189
  • 2
  • 5
  • 16
  • possible duplicate http://stackoverflow.com/questions/19295073/windowmanager-mainactivity-has-leaked-window-com-android-internal-policy-impl-p/19295140#19295140 – kalyan pvs Apr 07 '14 at 10:51
  • I have added the pDialog.dismiss() ,But still getting the error – sheetal Apr 07 '14 at 10:59
  • add it in the onStop or OnDestroy of the Activity..once check that link.. – kalyan pvs Apr 07 '14 at 11:02
  • watch for pDialog.dismiss(), show(); I'm pretty sure that you're trying to hide something that doesn't exist (at a moment); Inspect element in debug mode. Cheers – Avicena00 Apr 07 '14 at 13:05

1 Answers1

0

Use below code in onPostExecute()

if(pDialog.isShowing())
{
     pDialog.dismiss();
}
  • And what will it do? How exactly would this solve the problem? Could you please explain? – Rachcha Apr 09 '14 at 06:05
  • @Lovely after trying your suggestion I am getting a new error "Error converting result java.lang.NullPointerException:lock==null" – sheetal Apr 09 '14 at 06:06