简体   繁体   中英

Activity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{42de2350

Read all the articles about this problem but can't seem to find one that matches my situation, here is the logcat:

02-18 18:56:16.872    7331-7331/com.familiestvw.whatson E/WindowManager﹕ Activity com.familiestvw.whatson.GetData has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{42de2350 V.E..... R......D 0,0-1026,288} that was originally added here
android.view.WindowLeaked: Activity com.familiestvw.whatson.GetData has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{42de2350 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.GetData$ProgressTask.onPreExecute(GetData.java:64)
        at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
        at android.os.AsyncTask.execute(AsyncTask.java:534)
        at com.familiestvw.whatson.GetData.onCreate(GetData.java:45)
        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 code:

public class GetData extends ListActivity {
private Context context;
private static String url = "http://localhost/android_connect/get_venues.php";

private static final String lblListing_ID = "Listing_ID";
private static final String lblEvent_ID = "Event_ID";
private static final String lblVenue_ID = "Venue_ID";
private static final String lblStart_Date = "Start_Date";
private static final String lblEnd_Date = "End_Date";
private static final String lblFrequency = "Frequency";

ArrayList<HashMap<String, String>> jsonlist = new ArrayList<HashMap<String, String>>();

ListView lv ;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_results);
    ProgressTask task = new ProgressTask();
    task.execute();
}


public class ProgressTask extends AsyncTask<String, Void, Boolean> {
    ProgressDialog dialog;

    protected void onPreExecute() {
        dialog= ProgressDialog.show(GetData.this, "Please Wait","Searching Database", true);
    }

    protected Boolean doInBackground(final String... args) {

        JSONParser jParser = new JSONParser();

        // get JSON data from URL
        JSONArray json = jParser.getJSONFromUrl(url);

        for (int i = 0; i < json.length(); i++) {

            try {
                JSONObject c = json.getJSONObject(i);
                String Listing_ID = c.getString(lblListing_ID);
                String Event_ID = c.getString(lblEvent_ID);
                String Venue_ID = c.getString(lblVenue_ID);
                String Start_Date = c.getString(lblStart_Date);
                String End_Date = c.getString(lblEnd_Date);
                String Frequency = c.getString(lblFrequency);

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

                // Add child node to HashMap key & value
                map.put(lblListing_ID, Listing_ID);
                map.put(lblEvent_ID, Event_ID);
                map.put(lblVenue_ID, Venue_ID);
                map.put(lblStart_Date, Start_Date);
                map.put(lblEnd_Date, End_Date);
                map.put(lblFrequency, Frequency);
                jsonlist.add(map);
            }
            catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    @Override
    protected void onPostExecute(final Boolean success) {
            dialog.dismiss();
        ListAdapter adapter = new SimpleAdapter(context, jsonlist,
                R.layout.list_item, new String[] { lblListing_ID , lblEvent_ID,
                lblVenue_ID, lblStart_Date, lblEnd_Date, lblFrequency }, new int[] {
                R.id.Listing_ID, R.id.Event_ID, R.id.Venue_ID,
                R.id.Start_Date, R.id.End_Date, R.id.Frequency });

        setListAdapter(adapter);

        // select single ListView item
        lv = getListView();
    }


}

} Guessing the problem is likely to be with dismissing the dialog but can't seem to get a working solution.

Any suggestions would be much appreciated>

You're creating your progress dialog in your onCreate and immediately showing it, but your Activity has not passed through the onResume state yet. My suggestion would be to separate your progress dialog out to be started by the Activity and have your AsyncTask just fetch data and process it. When it is done, its postExecute callback can send a message to a Handler running on the UI thread to dismiss the dialog. Also note that AsyncTask is not lifecycle aware, so it is possible with the current approach to run into state problems and memory leaks if the Activity is paused or destroyed while the AsyncTask is still running in the background.

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