活动已泄露窗口com.android.internal.policy.impl.PhoneWindow $ DecorView {42de2350

时间:2014-02-18 19:04:00

标签: java android dialog window

阅读有关此问题的所有文章,但似乎找不到符合我情况的文章,这里是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)

这是我的代码:

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();
    }


}

} 猜测问题很可能是解雇对话但似乎无法找到有效的解决方案。

任何建议都会非常感谢&gt;

1 个答案:

答案 0 :(得分:1)

您正在onCreate中创建进度对话框并立即显示,但您的Activity尚未通过onResume状态。我的建议是将您的进度对话框分开,由Activity启动,让您的AsyncTask只获取数据并进行处理。完成后,其postExecute回调可以向在UI线程上运行的Handler发送消息以关闭该对话框。另请注意,AsyncTask不能识别生命周期,因此如果在AsyncTask仍在后台运行时暂停或销毁Activity,则当前方法可能会遇到状态问题和内存泄漏。