-1

I want to get data from server and display the data inside a listview. The application stops once it is running. I don't know which part went wrong. My apps stops with the following logcat messages:

10-08 07:45:56.996: D/OpenGLRenderer(1446): Enabling debug mode 0
10-08 07:45:57.092: W/EGL_genymotion(1446): eglSurfaceAttrib not implemented
10-08 07:46:02.356: I/Choreographer(1446): Skipped 315 frames!  The application may be doing too much work on its main thread.
10-08 07:46:07.424: I/Choreographer(1446): Skipped 303 frames!  The application may be doing too much work on its main thread.
10-08 07:46:07.716: E/WindowManager(1446): android.view.WindowLeaked: Activity com.zanqi.datatolistview.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{527e8d54 V.E..... R......D 0,0-1664,192} that was originally added here
10-08 07:46:07.716: E/WindowManager(1446):  at android.view.ViewRootImpl.<init>(ViewRootImpl.java:348)
10-08 07:46:07.716: E/WindowManager(1446):  at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:248)
10-08 07:46:07.716: E/WindowManager(1446):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
10-08 07:46:07.716: E/WindowManager(1446):  at android.app.Dialog.show(Dialog.java:286)
10-08 07:46:07.716: E/WindowManager(1446):  at com.zanqi.datatolistview.MainActivity$ProgressTask.onPreExecute(MainActivity.java:64)
10-08 07:46:07.716: E/WindowManager(1446):  at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:587)
10-08 07:46:07.716: E/WindowManager(1446):  at android.os.AsyncTask.execute(AsyncTask.java:535)
10-08 07:46:07.716: E/WindowManager(1446):  at com.zanqi.datatolistview.MainActivity.onCreate(MainActivity.java:43)
10-08 07:46:07.716: E/WindowManager(1446):  at android.app.Activity.performCreate(Activity.java:5231)
10-08 07:46:07.716: E/WindowManager(1446):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
10-08 07:46:07.716: E/WindowManager(1446):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
10-08 07:46:07.716: E/WindowManager(1446):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
10-08 07:46:07.716: E/WindowManager(1446):  at android.app.ActivityThread.access$800(ActivityThread.java:135)
10-08 07:46:07.716: E/WindowManager(1446):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
10-08 07:46:07.716: E/WindowManager(1446):  at android.os.Handler.dispatchMessage(Handler.java:102)
10-08 07:46:07.716: E/WindowManager(1446):  at android.os.Looper.loop(Looper.java:136)
10-08 07:46:07.716: E/WindowManager(1446):  at android.app.ActivityThread.main(ActivityThread.java:5017)
10-08 07:46:07.716: E/WindowManager(1446):  at java.lang.reflect.Method.invokeNative(Native Method)
10-08 07:46:07.716: E/WindowManager(1446):  at java.lang.reflect.Method.invoke(Method.java:515)
10-08 07:46:07.716: E/WindowManager(1446):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
10-08 07:46:07.716: E/WindowManager(1446):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
10-08 07:46:07.716: E/WindowManager(1446):  at dalvik.system.NativeStart.main(Native Method)

Could someone advise me what is wrong with my codes? my codes are :

public class MainActivity extends ListActivity {

private Context context; 
private static String url = ""; 
private static final String NUMBER = "Number"; 
private static final String INFO = "Info"; 
private static final String REMARKS = "Remarks"; 


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_main); 
    new ProgressTask(MainActivity.this).execute(); } 


private class ProgressTask extends AsyncTask<String, Void, Boolean> { 

    private ProgressDialog dialog; 

    private ListActivity activity; 

    // private List<Message> messages; 

    public ProgressTask(ListActivity activity) { 

        this.activity = activity; 
        context = activity; 
        dialog = new ProgressDialog(context); } 

    private Context context; 

    protected void onPreExecute() { 
        this.dialog.setMessage("Progress start"); 
        this.dialog.show(); } 

    @Override 
    protected void onPostExecute(final Boolean success) { 
        if (dialog.isShowing()) { dialog.dismiss(); } 
        ListAdapter adapter = new SimpleAdapter(context, jsonlist, R.layout.list_item, new String[] { NUMBER, INFO, REMARKS }, 
                new int[] { R.id.vehicleType, R.id.vehicleColor, R.id.fuel }); setListAdapter(adapter); 
                //select single ListView item 
                lv = getListView(); } 
    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 number = c.getString(NUMBER); 
                String info = c.getString(INFO); 
                String remarks = c.getString(REMARKS); 

                HashMap<String, String> map = new HashMap<String, String>(); 
                // Add child node to HashMap key & value 

                map.put(NUMBER, number); 
                map.put(INFO, info); 
                map.put(REMARKS, remarks); 

                jsonlist.add(map); 

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

    }
}


public class JSONParser {

static InputStream iStream = null; 
static JSONArray jarray = null; 
static String json = ""; 
public JSONParser() { } 
public JSONArray getJSONFromUrl(String url) { 
    StringBuilder builder = new StringBuilder(); 
    HttpClient client = new DefaultHttpClient(); 
    HttpGet httpGet = new HttpGet(url); 
    try { 
        HttpResponse response = client.execute(httpGet); 
        StatusLine statusLine = response.getStatusLine(); 
        int statusCode = statusLine.getStatusCode(); 
        if (statusCode == 200) { 
            HttpEntity entity = response.getEntity(); 
            InputStream content = entity.getContent(); 
            BufferedReader reader = new BufferedReader(new InputStreamReader(content)); 
            String line; 
            while ((line = reader.readLine()) != null) { builder.append(line); } } 
        else { Log.e("==>", "Failed to download file"); } } 
    catch (ClientProtocolException e) { e.printStackTrace(); } 
    catch (IOException e) { e.printStackTrace(); } 
    // Parse String to JSON object 
    try { 
        jarray = new JSONArray( builder.toString());
        } catch (JSONException e) { 
            Log.e("JSON Parser", "Error parsing data " + e.toString()); }
    // return JSON Object 
    return jarray; 
}

}

NEWBIE
  • 33
  • 2
  • 7
  • i already include if (dialog.isShowing()) { dialog.dismiss(); } in my program... why still leak? – NEWBIE Oct 08 '14 at 08:12

3 Answers3

0

It is a Leaked Window error. It happens when some UI function access the activity when it is in background.

In your MainActivity on Line 72 on onPreExecute method check this,

  if(dialog != null && !activity.isFinishing()) {
     this.dialog.setMessage("Progress start"); 
     this.dialog.show(); 
  }

This checks if activity is not in background and Dialog is not null, then show it.

Bharath Mg
  • 1,117
  • 1
  • 9
  • 18
0

There are two possible answers:

  1. You're trying to show a Dialog after you've exited an Activity.
  2. This error can be a little misleading in some circumstances - i.e. in my case an unhandled Exception was thrown in an AsyncTask, which caused the Activity to shutdown, then an open progress dialog caused this Exception.. so the 'real' exception was a little earlier in the log

Look at: Activity has leaked window that was originally added

Community
  • 1
  • 1
An SO User
  • 24,612
  • 35
  • 133
  • 221
0

You can modify the UI Interface from an AsyncTask as you do in onPreExcecute.

MainActivity.this.runOnUiThread(new Runnable() {
    public void run() {
       dialog.setMessage("Progress start"); 
       dialog.show();
    }
});
Maxime
  • 1,332
  • 1
  • 15
  • 43