我在我的 Android 应用程序中使用 MapQuest jar,我刚刚遇到了 RouteManager.RouteCallback() 所需的方法之一的问题,该方法是 MapQuest jar 的一部分。
给我 NullPointerException 的方法是 onError() 方法,您将在下面的代码中看到。如何从该方法中捕获 NullPointerException 以使应用程序不会崩溃?我尝试将整个方法包装在 try/catch 中,但这不起作用,它给了我错误。有什么建议么?谢谢。
routeManager.setRouteCallback(new RouteManager.RouteCallback() {
@Override
public void onError(RouteResponse routeResponse) {
Info info = routeResponse.info;
int statusCode = info.statusCode;
StringBuilder message = new StringBuilder();
message.append("Unable to create route.\n").append("Error: ")
.append(statusCode).append("\n").append("Message: ")
.append(info.messages);
Toast.makeText(getApplicationContext(), message.toString(),
Toast.LENGTH_LONG).show();
createRouteButton.setEnabled(true);
}
@Override
public void onSuccess(RouteResponse routeResponse) {
clearButton.setVisibility(View.VISIBLE);
if (showItineraryButton.getVisibility() == View.GONE
&& showMapButton.getVisibility() == View.GONE) {
showItineraryButton.setVisibility(View.VISIBLE);
}
createRouteButton.setEnabled(true);
}
});
}
堆栈跟踪:
01-11 14:26:51.296: E/AndroidRuntime(343): FATAL EXCEPTION: main
01-11 14:26:51.296: E/AndroidRuntime(343): java.lang.NullPointerException: println needs a message
01-11 14:26:51.296: E/AndroidRuntime(343): at android.util.Log.println_native(Native Method)
01-11 14:26:51.296: E/AndroidRuntime(343): at android.util.Log.e(Log.java:230)
01-11 14:26:51.296: E/AndroidRuntime(343): at com.mapquest.android.maps.RouteManager.handleResponse(RouteManager.java:393)
01-11 14:26:51.296: E/AndroidRuntime(343): at com.mapquest.android.maps.RouteManager.access$400(RouteManager.java:59)
01-11 14:26:51.296: E/AndroidRuntime(343): at com.mapquest.android.maps.RouteManager$RouteTask.onPostExecute(RouteManager.java:648)
01-11 14:26:51.296: E/AndroidRuntime(343): at com.mapquest.android.maps.RouteManager$RouteTask.onPostExecute(RouteManager.java:611)
01-11 14:26:51.296: E/AndroidRuntime(343): at android.os.AsyncTask.finish(AsyncTask.java:417)
01-11 14:26:51.296: E/AndroidRuntime(343): at android.os.AsyncTask.access$300(AsyncTask.java:127)
01-11 14:26:51.296: E/AndroidRuntime(343): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
01-11 14:26:51.296: E/AndroidRuntime(343): at android.os.Handler.dispatchMessage(Handler.java:99)
01-11 14:26:51.296: E/AndroidRuntime(343): at android.os.Looper.loop(Looper.java:123)
01-11 14:26:51.296: E/AndroidRuntime(343): at android.app.ActivityThread.main(ActivityThread.java:3683)
01-11 14:26:51.296: E/AndroidRuntime(343): at java.lang.reflect.Method.invokeNative(Native Method)
01-11 14:26:51.296: E/AndroidRuntime(343): at java.lang.reflect.Method.invoke(Method.java:507)
01-11 14:26:51.296: E/AndroidRuntime(343): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-11 14:26:51.296: E/AndroidRuntime(343): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-11 14:26:51.296: E/AndroidRuntime(343): at dalvik.system.NativeStart.main(Native Method)