简体   繁体   中英

Activity has leaked window com.android.internal.policy.impl.PhoneWindows$DecorView{42d9a800

im writting an app that gets data from a database however im getting this error:

02-10 11:37:08.779  19458-19458/com.familiestvw.whatson E/WindowManager﹕ Activity com.familiestvw.whatson.AllVenuesActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{42d9a800 V.E..... R......D 0,0-1026,288} that was originally added here
android.view.WindowLeaked: Activity com.familiestvw.whatson.AllVenuesActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{42d9a800 V.E..... R......D 0,0-1026,288} that was originally added here
        at android.view.ViewRootImpl.<init>(ViewRootImpl.java:450)
        at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:258)
        at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:73)
        at android.app.Dialog.show(Dialog.java:287)
        at com.familiestvw.whatson.AllVenuesActivity$LoadAllVenues.onPreExecute(AllVenuesActivity.java:74)
        at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
        at android.os.AsyncTask.execute(AsyncTask.java:534)
        at com.familiestvw.whatson.AllVenuesActivity.onCreate(AllVenuesActivity.java:53)
        at android.app.Activity.performCreate(Activity.java:5372)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2257)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
        at android.app.ActivityThread.access$700(ActivityThread.java:159)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5419)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:525)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
        at dalvik.system.NativeStart.main(Native Method)

Here is my Java code, sorry its the whole activity but i literally have no idea where the error is occuring or even what it is, im kinda new to android:

package com.familiestvw.whatson;


public class AllVenuesActivity extends ListActivity {

// Progress Dialog
private ProgressDialog pDialog;

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

ArrayList<HashMap<String, String>> venuesList;

// url to get all venues list
private static String url_all_venues = "http://10.0.2.2/android_connect/get_all_venues.php";

// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_VENUES = "venues";
private static final String TAG_VENUE_ID = "Venue_ID";
private static final String TAG_VENUE_NAME = "Venue_Name";

// venues JSONArray
JSONArray venues = null;

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

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

    // Loading venues in Background Thread
    new LoadAllVenues().execute();

    // Get listview
    ListView lv = getListView();
    }

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

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

    /**
     * getting All venues 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_venues, "GET", params);

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

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

            if (success == 1) {
                // venues found
                // Getting Array of Venues
                venues = json.getJSONArray(TAG_VENUES);

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

                    // Storing each json item in variable
                    String id = c.getString(TAG_VENUE_ID);
                    String name = c.getString(TAG_VENUE_NAME);

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

                    // adding each child node to HashMap key => value
                    map.put(TAG_VENUE_ID, id);
                    map.put(TAG_VENUE_NAME, name);

                    // adding HashList to ArrayList
                    venuesList.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(
                        AllVenuesActivity.this, venuesList,
                        R.layout.list_item, new String[] { TAG_VENUE_ID,
                        TAG_VENUE_NAME},
                        new int[] { R.id.Venue_ID, R.id.Venue_Name });
                // updating listview
                setListAdapter(adapter);
            }
        });

    }

 }
}

Any ideas??

It usually happens when you are trying try to dismiss any dialog which is no longer created or exist.

Possible reasons are

  • Your activity is no longer exists but your task is still running and trying to dismiss the dialog.

  • Your app is crashing somewhere in doInBackgrond.

Check values of yopur JsonObject "c" and better to add null checks for JsonObject or wherever there is a chance for NullPointerException occurrence in your code (doInbackground here).

onPostExcute runs in Main/UI thread not in background thread.

EDIT :

Replace your doInBackground code with this and make sure you are not switching the Activity in between the task is running :

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

    if(json!=null){
    // Check your log cat for JSON reponse
        Log.d("All Venues: ", json.toString());
        try {
            // Checking for SUCCESS TAG
            int success = json.getInt(TAG_SUCCESS);
            if (success == 1) {
            // venues found
            // Getting Array of Venues
                venues = json.getJSONArray(TAG_VENUES);
                // looping through All Venues
                if(venues !=null){
                    for (int i = 0; i < venues.length(); i++) {
                        JSONObject c = venues.getJSONObject(i);
                        // Storing each json item in variable
                        if(c!=null){
                            String id = c.getString(TAG_VENUE_ID);
                            String name = c.getString(TAG_VENUE_NAME);
                        }
                        // creating new HashMap
                        HashMap<String, String> map = new HashMap<String, String>();
                        // adding each child node to HashMap key => value
                        map.put(TAG_VENUE_ID, id);
                        map.put(TAG_VENUE_NAME, name);
                         // adding HashList to ArrayList
                        venuesList.add(map);
                    }   
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
    return null;
}

I recommended you to use one General class for ex : Utils.java By using this class you can use progressDialog in any activity.

Utils.java

private static ProgressDialog progressDialog = null;

public static void showProgressDialog(Context context, String title, String message)
{
        progressDialog = new ProgressDialog(context);
        progressDialog.setTitle(title);
        progressDialog.setMessage(message);
        progressDialog.setCancelable(false);
        progressDialog.show();  
}

public static void dismissProgressDialog()
{
    progressDialog.dismiss();
}
public static void clearDialog()
{
    progressDialog = null;
}

And to show progressDialog call :

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

    /**
     * Before starting background thread Show Progress Dialog
     * */
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        Utils.clearDialog();
        Utils.showProgressDialog(Youactivity.this, "Title", "Please wait...");
    }

    /**
     * getting All venues 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_venues, "GET", params);

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

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

            if (success == 1) {
                // venues found
                // Getting Array of Venues
                venues = json.getJSONArray(TAG_VENUES);

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

                    // Storing each json item in variable
                    String id = c.getString(TAG_VENUE_ID);
                    String name = c.getString(TAG_VENUE_NAME);

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

                    // adding each child node to HashMap key => value
                    map.put(TAG_VENUE_ID, id);
                    map.put(TAG_VENUE_NAME, name);

                    // adding HashList to ArrayList
                    venuesList.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
        Utils.dismissProgressDialog();
        // updating UI from Background Thread
        runOnUiThread(new Runnable() {
            public void run() {
                /**
                 * Updating parsed JSON data into ListView
                 * */
                ListAdapter adapter = new SimpleAdapter(
                        AllVenuesActivity.this, venuesList,
                        R.layout.list_item, new String[] { TAG_VENUE_ID,
                        TAG_VENUE_NAME},
                        new int[] { R.id.Venue_ID, R.id.Venue_Name });
                // updating listview
                setListAdapter(adapter);
            }
        });

    }

 }

This error will happened if your activity has been destroyed but you dialog is still showing. So You have added these code in your activity's onDestroy()

@Override
public void OnDestory() {
if (dialog != null) {
    dialog.dismiss();
    dialog = null;
}

}

it may help you.

Use this below code in your onPostExecute method.

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

So your method will look something like this.

protected void onPostExecute(String file_url) {
        // dismiss the dialog after getting all products
        Utils.dismissProgressDialog();

 if (pDialog!= null) {
    pDialog.dismiss();
    pDialog= null;
}
        // updating UI from Background Thread
        runOnUiThread(new Runnable() {
            public void run() {
                /**
                 * Updating parsed JSON data into ListView
                 * */
                ListAdapter adapter = new SimpleAdapter(
                        AllVenuesActivity.this, venuesList,
                        R.layout.list_item, new String[] { TAG_VENUE_ID,
                        TAG_VENUE_NAME},
                        new int[] { R.id.Venue_ID, R.id.Venue_Name });
                // updating listview
                setListAdapter(adapter);
            }
        });

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM