From 5fd1e2e734667f8ba9b60272c28280a624a8bbad Mon Sep 17 00:00:00 2001 From: Arndt Date: Wed, 30 Jul 2014 08:19:45 +0200 Subject: [PATCH] fixed nullpointer at startup --- .../main/java/btools/routingapp/AppLogger.java | 16 +++++++++++++++- .../java/btools/routingapp/BRouterActivity.java | 1 + .../main/java/btools/routingapp/BRouterView.java | 9 ++++++++- 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/brouter-routing-app/src/main/java/btools/routingapp/AppLogger.java b/brouter-routing-app/src/main/java/btools/routingapp/AppLogger.java index 7e75fef..586d1f4 100644 --- a/brouter-routing-app/src/main/java/btools/routingapp/AppLogger.java +++ b/brouter-routing-app/src/main/java/btools/routingapp/AppLogger.java @@ -3,6 +3,8 @@ package btools.routingapp; import java.io.File; import java.io.FileWriter; import java.io.IOException; +import java.io.PrintWriter; +import java.io.StringWriter; import java.util.Date; import android.os.Environment; @@ -61,8 +63,20 @@ public class AppLogger } catch( IOException e ) { - throw new RuntimeException( "cannot write appdebug.txt: " + e ); + throw new RuntimeException( "cannot write brouterapp.txt: " + e ); } } } + + /** + * Format an exception using + */ + public static String formatThrowable( Throwable t ) + { + StringWriter sw = new StringWriter(); + PrintWriter pw = new PrintWriter( sw ); + t.printStackTrace( pw ); + return sw.toString(); + } + } diff --git a/brouter-routing-app/src/main/java/btools/routingapp/BRouterActivity.java b/brouter-routing-app/src/main/java/btools/routingapp/BRouterActivity.java index b98d8c5..ae10e45 100644 --- a/brouter-routing-app/src/main/java/btools/routingapp/BRouterActivity.java +++ b/brouter-routing-app/src/main/java/btools/routingapp/BRouterActivity.java @@ -56,6 +56,7 @@ public class BRouterActivity extends Activity implements OnInitListener { // instantiate our simulation view and set it as the activity's content mBRouterView = new BRouterView(this); + mBRouterView.init(); setContentView(mBRouterView); } diff --git a/brouter-routing-app/src/main/java/btools/routingapp/BRouterView.java b/brouter-routing-app/src/main/java/btools/routingapp/BRouterView.java index 3729815..124725e 100644 --- a/brouter-routing-app/src/main/java/btools/routingapp/BRouterView.java +++ b/brouter-routing-app/src/main/java/btools/routingapp/BRouterView.java @@ -83,7 +83,10 @@ public class BRouterView extends View public BRouterView(Context context) { super(context); - + } + + public void init() + { DisplayMetrics metrics = new DisplayMetrics(); ((Activity)getContext()).getWindowManager().getDefaultDisplay().getMetrics(metrics); imgw = metrics.widthPixels; @@ -218,6 +221,10 @@ public class BRouterView extends View String msg = e instanceof IllegalArgumentException ? e.getMessage() + ( cor == null ? "" : " (coordinate-source: " + cor.basedir + cor.rootdir + ")" ) : e.toString(); + + AppLogger.log( msg ); + AppLogger.log( AppLogger.formatThrowable( e ) ); + ((BRouterActivity)getContext()).showErrorMessage( msg ); } waitingForSelection = true;