--- android/TrainInfo/src/dk/thoerup/traininfo/StationList.java 2010/05/03 12:39:34 700 +++ android/TrainInfo/src/dk/thoerup/traininfo/StationList.java 2010/05/03 13:41:04 701 @@ -119,7 +119,7 @@ startLookup(); break; case ListSearch: - this.showDialog(DLG_STATIONNAME); + this.showDialogSafe(DLG_STATIONNAME); break; case ListFavorites: startFavoriteLookup(); @@ -152,8 +152,41 @@ } setTitle(dialogTitle); + + } + + + /* these 3 dialogs helper functions are very rude and ugly hack + * to remove these auto-reported exceptions + * - android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@436aaef8 is not valid; is your activity running? + * - java.lang.IllegalArgumentException: View not attached to window manager + */ + + public void showDialogSafe(int id) { + try { + showDialog(id); + } catch (Exception e) { + Log.e("StationList", "showDialog failed", e); + } + } + + public void dismissDialogSafe(int id) { + try { + dismissDialog(id); + } catch (Exception e) { + Log.e("StationList", "dismissDialog failed", e); + } } + public void builderShowSafe(AlertDialog.Builder builder) { + try { + builder.show(); + } catch (Exception e) { + Log.e("StationList", "builder.show() failed", e); + } + + } + /* EOF rude and ugly dialog hack */ @@ -324,7 +357,7 @@ public void startLookup() { isRunning = true; dialogMessage = getString( stationlist_waitforlocation ); - showDialog(DLG_PROGRESS); + showDialogSafe(DLG_PROGRESS); locationLookup.locateStations(); stationsFetched.sendEmptyMessageDelayed(LOCATIONFIXTIMEOUT, 20000); @@ -332,7 +365,7 @@ void startNameSearch(String name) { dialogMessage = getString( stationlist_findbyname ); - showDialog(DLG_PROGRESS); + showDialogSafe(DLG_PROGRESS); findStationsTask = new FindStationsTask(); findStationsTask.searchByName(name); @@ -344,7 +377,7 @@ if (favorites.size() > 0) { dialogMessage = getString( stationlist_loadfavorites ); - showDialog(DLG_PROGRESS); + showDialogSafe(DLG_PROGRESS); findStationsTask = new FindStationsTask(); findStationsTask.searchByIds( favorites.toString() ); @@ -359,7 +392,7 @@ void startLocatorTask() { dialogMessage = getString( stationlist_findingnearby ); - showDialog(DLG_PROGRESS); + showDialogSafe(DLG_PROGRESS); findStationsTask = new FindStationsTask(); findStationsTask.searchByLocation( locationLookup.getLocation() ); @@ -376,7 +409,7 @@ switch (msg.what) { case GOTLOCATION: - dismissDialog(DLG_PROGRESS); + dismissDialogSafe(DLG_PROGRESS); startLocatorTask(); location = GeoPair.fromLocation( locationLookup.getLocation() ); @@ -384,7 +417,7 @@ break; case NOPROVIDER: - dismissDialog(DLG_PROGRESS); + dismissDialogSafe(DLG_PROGRESS); MessageBox.showMessage(StationList.this, getString(stationlist_nolocationprovider) ); break; case LOCATIONFIXTIMEOUT: @@ -393,7 +426,7 @@ if (locationLookup.hasLocation()) { stationsFetched.sendEmptyMessage( GOTLOCATION ); } else { - dismissDialog(DLG_PROGRESS); + dismissDialogSafe(DLG_PROGRESS); AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this); builder.setMessage( getString( stationlist_gpstimeout) ); @@ -409,8 +442,8 @@ public void onClick(DialogInterface dialog, int id) { dialog.dismiss(); } - }); - builder.show(); + }); + builderShowSafe(builder); // builder.show() } } @@ -531,12 +564,9 @@ public void onClick(DialogInterface dialog, int id) { dialog.dismiss(); } - }); - try { - builder.show(); - } catch (android.view.WindowManager.BadTokenException e) { - Log.i("StationList", "BadTokenException"); // this can happen if the user switched away from this activity, while doInBackground was running - } + }); + + builderShowSafe(builder); // builder.show() } } }