exception - Android Leaked Window - App adopts an ANR State -
i working android app uses google maps. application works fine often. able see errors in logcat [below].
logcat
08-28 15:37:36.945: e/windowmanager(21969): activity com.kod.example.find_tab_mainactivity has leaked window com.android.internal.policy.impl.phonewindow$decorview@40929cd8 added here 08-28 15:37:36.945: e/windowmanager(21969): android.view.windowleaked: activity com.kod.example.find_tab_mainactivity has leaked window com.android.internal.policy.impl.phonewindow$decorview@40929cd8 added here 08-28 15:37:36.945: e/windowmanager(21969): @ android.view.viewroot.<init>(viewroot.java:263) 08-28 15:37:36.945: e/windowmanager(21969): @ android.view.windowmanagerimpl.addview(windowmanagerimpl.java:171) 08-28 15:37:36.945: e/windowmanager(21969): @ android.view.windowmanagerimpl.addview(windowmanagerimpl.java:114) 08-28 15:37:36.945: e/windowmanager(21969): @ android.view.window$localwindowmanager.addview(window.java:424) 08-28 15:37:36.945: e/windowmanager(21969): @ android.app.dialog.show(dialog.java:241) 08-28 15:37:36.945: e/windowmanager(21969): @ com.kod.example.myprogressdialog.show(myprogressdialog.java:36) 08-28 15:37:36.945: e/windowmanager(21969): @ com.kod.example.myprogressdialog.show(myprogressdialog.java:19) 08-28 15:37:36.945: e/windowmanager(21969): @ com.kod.example.myprogressdialog.show(myprogressdialog.java:14) 08-28 15:37:36.945: e/windowmanager(21969): @ com.kod.example.find_tab_mainactivity$loadplaces.onpreexecute(find_tab_mainactivity.java:129) 08-28 15:37:36.945: e/windowmanager(21969): @ android.os.asynctask.execute(asynctask.java:391) 08-28 15:37:36.945: e/windowmanager(21969): @ com.kod.example.find_tab_mainactivity.oncreate(find_tab_mainactivity.java:112) 08-28 15:37:36.945: e/windowmanager(21969): @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1047) 08-28 15:37:36.945: e/windowmanager(21969): @ android.app.activitythread.performlaunchactivity(activitythread.java:1615) 08-28 15:37:36.945: e/windowmanager(21969): @ android.app.activitythread.handlelaunchactivity(activitythread.java:1667) 08-28 15:37:36.945: e/windowmanager(21969): @ android.app.activitythread.access$1500(activitythread.java:117) 08-28 15:37:36.945: e/windowmanager(21969): @ android.app.activitythread$h.handlemessage(activitythread.java:935) 08-28 15:37:36.945: e/windowmanager(21969): @ android.os.handler.dispatchmessage(handler.java:99) 08-28 15:37:36.945: e/windowmanager(21969): @ android.os.looper.loop(looper.java:130) 08-28 15:37:36.945: e/windowmanager(21969): @ android.app.activitythread.main(activitythread.java:3687) 08-28 15:37:36.945: e/windowmanager(21969): @ java.lang.reflect.method.invokenative(native method) 08-28 15:37:36.945: e/windowmanager(21969): @ java.lang.reflect.method.invoke(method.java:507) 08-28 15:37:36.945: e/windowmanager(21969): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:867) 08-28 15:37:36.945: e/windowmanager(21969): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:625) 08-28 15:37:36.945: e/windowmanager(21969): @ dalvik.system.nativestart.main(native method)
what leaked window? app works fine crashes, throws error.
my code
package com.kod.example; import java.util.list; import android.app.alertdialog; import android.app.progressdialog; import android.content.context; import android.content.dialoginterface; import android.content.intent; import android.location.address; import android.location.geocoder; import android.os.asynctask; import android.os.bundle; import android.widget.button; import com.google.android.maps.mapactivity; public class find_tab_mainactivity extends mapactivity { // flag internet connection status boolean isinternetpresent = false; geocoder geocoder; string status; // connection detector class connectiondetector cd; // alert dialog manager alertdialogmanager alert = new alertdialogmanager(); // google places googleplaces googleplaces; // places list placeslist nearplaces; place reference; // gps location gpstracker gps; double latitude; double longitude; // button button loginbtn; place p; intent i; // progress dialog progressdialog pdialog; final context context = this; string type,key_keyword,key_tag; // key strings public static string key_reference = "reference"; // id of place public static string key_name = "name"; // name of place public static string key_vicinity = "vicinity"; public static string key_number = "formatted_phone_number"; // place area name @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); cd = new connectiondetector(getapplicationcontext()); intent entent = getintent(); type = entent.getextras().getstring("key_types"); key_keyword = entent.getextras().getstring("key_keyword"); key_tag = entent.getextras().getstring("key_tag"); // check if internet present isinternetpresent = cd.isconnectingtointernet(); if (!isinternetpresent) { // internet connection not present alert.showalertdialog(find_tab_mainactivity.this, "internet connection error", "please connect working internet connection", false); // stop executing code return return; } // creating gps class object gps = new gpstracker(this); // check if gps location can if (gps.cangetlocation()) { } else { // can't user's current location alert.showalertdialog(find_tab_mainactivity.this, "gps status", "couldn't location information. please enable gps", false); // stop executing code return return; } // calling background async task load google places // after getting places google data shown in listview new loadplaces().execute(); /** button click event shown on map */ } /** * background async task load google places * */ class loadplaces extends asynctask<string, string, string> { /** * before starting background thread show progress dialog * */ @override protected void onpreexecute() { super.onpreexecute(); myprogressdialog.show(find_tab_mainactivity.this, "", ""); } /** * getting places json * */ protected string doinbackground(string... args) { // creating places class object googleplaces = new googleplaces(); try { // separeate place types pipe symbol "|" // if want types places make null // check list of types supported google string types = type; // listing places park // radius in meters - increase value if don't find places double radius = 10000; // 1000 meters if(key_tag.equalsignorecase("1")||key_tag.equalsignorecase("4")||key_tag.equalsignorecase("5")) { // nearest places nearplaces = googleplaces.search(gps.getlatitude(),gps.getlongitude(), radius, types); } else if(key_tag.equalsignorecase("2")||key_tag.equalsignorecase("3")) { // nearest places nearplaces = googleplaces.streetgaragesearch(gps.getlatitude(), gps.getlongitude(), radius, types,key_keyword); } else if(key_tag.equalsignorecase("6")) { string addressinput = type; geocoder = new geocoder(find_tab_mainactivity.this); list<address> address; address = geocoder.getfromlocationname(addressinput,1); if (address == null) { alert.showalertdialog(find_tab_mainactivity.this, "places error", "sorry error occured.", false); } else{ address location = address.get(0); latitude = location.getlatitude(); longitude = location.getlongitude(); nearplaces = googleplaces.keywordsearch(latitude,longitude,radius+40000,"parking",type); } // nearest places } } catch (exception e) { e.printstacktrace(); } return null; } /** * after completing background task dismiss progress dialoghis * , show data in ui * use runonuithread(new runnable()) update ui background * thread, otherwise error * **/ @suppresswarnings("deprecation") protected void onpostexecute(string file_url) { try{ // json response status status = nearplaces.status; // updating ui background thread runonuithread(new runnable() { public void run() { if(status.equals("ok")){ // got places details = new intent(getapplicationcontext(), myplacesmapactivity.class); // sending user current geo location // passing near places map activity i.putextra("near_places", nearplaces); //passing type placemapactivity identify overlay icon i.putextra("key_tag", key_tag); //passing place reference i.putextra("place_reference", reference); // staring activity startactivity(i); } else if(status.equals("zero_results")){ // 0 results found final alertdialog alertdialog = new alertdialog.builder(context).create(); // setting dialog title alertdialog.settitle("no results"); // setting dialog message alertdialog.setmessage("sorry no places found. try change location"); // setting alert dialog icon alertdialog.seticon(r.drawable.fail); // setting ok button alertdialog.setbutton("ok", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int which) { find_tab_mainactivity.this.finish(); } }); // showing alert message alertdialog.show(); } else if(status.equals("unknown_error")) { final alertdialog alertdialog = new alertdialog.builder(context).create(); // setting dialog title alertdialog.settitle("error"); // setting dialog message alertdialog.setmessage("sorry unknown error occured."); // setting alert dialog icon alertdialog.seticon(r.drawable.fail); // setting ok button alertdialog.setbutton("ok", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int which) { find_tab_mainactivity.this.finish(); } }); // showing alert message alertdialog.show(); } else if(status.equals("over_query_limit")) { final alertdialog alertdialog = new alertdialog.builder(context).create(); // setting dialog title alertdialog.settitle("error"); // setting dialog message alertdialog.setmessage("sorry query limit reached"); // setting alert dialog icon alertdialog.seticon(r.drawable.fail); // setting ok button alertdialog.setbutton("ok", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int which) { find_tab_mainactivity.this.finish(); } }); // showing alert message alertdialog.show(); } else if(status.equals("request_denied")) { final alertdialog alertdialog = new alertdialog.builder(context).create(); // setting dialog title alertdialog.settitle("error"); // setting dialog message alertdialog.setmessage("sorry error occured. request denied"); // setting alert dialog icon alertdialog.seticon(r.drawable.fail); // setting ok button alertdialog.setbutton("ok", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int which) { find_tab_mainactivity.this.finish(); } }); // showing alert message alertdialog.show(); } else if(status.equals("invalid_request")) { final alertdialog alertdialog = new alertdialog.builder(context).create(); // setting dialog title alertdialog.settitle("error"); // setting dialog message alertdialog.setmessage("sorry error occured. invalid request"); // setting alert dialog icon alertdialog.seticon(r.drawable.fail); // setting ok button alertdialog.setbutton("ok", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int which) { find_tab_mainactivity.this.finish(); } }); // showing alert message alertdialog.show(); } else { final alertdialog alertdialog = new alertdialog.builder(context).create(); // setting dialog title alertdialog.settitle("error"); // setting dialog message alertdialog.setmessage("sorry error occured."); // setting alert dialog icon alertdialog.seticon(r.drawable.fail); // setting ok button alertdialog.setbutton("ok", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int which) { find_tab_mainactivity.this.finish(); } }); // showing alert message alertdialog.show(); } } }); }catch(nullpointerexception e){ final alertdialog alertdialog = new alertdialog.builder(context).create(); // setting dialog title alertdialog.settitle("error"); // setting dialog message alertdialog.setmessage("requested place not found."); // setting alert dialog icon alertdialog.seticon(r.drawable.fail); // setting ok button alertdialog.setbutton("ok", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int which) { find_tab_mainactivity.this.finish(); } }); // showing alert message alertdialog.show(); } } } @override protected boolean isroutedisplayed() { // todo auto-generated method stub return false; } }
what leaked window?
the windowleaked exception
in case happening because not dismissing myprogressdialog
before finishing activity. hence leaking memory associated it.
general case :
the windowleaked exception
happens when have sort of async task finishing after activity began it, destroyed.this type of exception occurred in case dialogs
,where did not dismiss dialogs before activity context destroyed.
so better put check before showing dialog, follows:
if(getactivity()!= null && !getactivity().isfinishing()){ dialog.show(); }
solution above issue dismiss
myprogressdialog
before finish activity.
modify code follows:
protected void onpostexecute(string file_url) { // first line should if ( myprogressdialog!=null && myprogressdialog.isshowing() ){ myprogressdialog.dismiss(); } // rest of code }
Comments
Post a Comment