我正在使用android maps API编写Android应用程序。 我有一个Button调用当前位置并尝试使用Geocoder getFromLocation函数打印地址。
当我点击按钮时,当我使用e.printStackTrace()
应用程序炸弹时没有任何反应。
以下是相关代码:
public void gpsToastButton(View view) {
Location gps = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (gps != null) {
int lat = (int) (gps.getLatitude() * 1E6);
int lng = (int) (gps.getLongitude() * 1E6);
Geocoder myLocation = new Geocoder(getApplicationContext(), Locale.getDefault());
try {
List<Address> myList = myLocation.getFromLocation(lat, lng, 1);
String message = String
.format("gps:" + myList,
gps.getLongitude(), gps.getLatitude()
);
Toast.makeText(GoogleMapActivity.this, message, Toast.LENGTH_LONG)
.show();
} catch (IOException e) {
// TODO Auto-generated catch block
String message2 = String
.format("kein anschluss unter dieser nummer");
Toast.makeText(GoogleMapActivity.this, message2, Toast.LENGTH_LONG);
}
}
else{...
}
单击按钮后和LogCat:
03-07 05:16:33.318: E/AndroidRuntime(883): FATAL EXCEPTION: main
03-07 05:16:33.318: E/AndroidRuntime(883): java.lang.IllegalStateException: Could not execute method of the activity
03-07 05:16:33.318: E/AndroidRuntime(883): at android.view.View$1.onClick(View.java:2072)
03-07 05:16:33.318: E/AndroidRuntime(883): at android.view.View.performClick(View.java:2408)
03-07 05:16:33.318: E/AndroidRuntime(883): at android.view.View$PerformClick.run(View.java:8816)
03-07 05:16:33.318: E/AndroidRuntime(883): at android.os.Handler.handleCallback(Handler.java:587)
03-07 05:16:33.318: E/AndroidRuntime(883): at android.os.Handler.dispatchMessage(Handler.java:92)
03-07 05:16:33.318: E/AndroidRuntime(883): at android.os.Looper.loop(Looper.java:123)
03-07 05:16:33.318: E/AndroidRuntime(883): at android.app.ActivityThread.main(ActivityThread.java:4627)
03-07 05:16:33.318: E/AndroidRuntime(883): at java.lang.reflect.Method.invokeNative(Native Method)
03-07 05:16:33.318: E/AndroidRuntime(883): at java.lang.reflect.Method.invoke(Method.java:521)
03-07 05:16:33.318: E/AndroidRuntime(883): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-07 05:16:33.318: E/AndroidRuntime(883): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-07 05:16:33.318: E/AndroidRuntime(883): at dalvik.system.NativeStart.main(Native Method)
03-07 05:16:33.318: E/AndroidRuntime(883): Caused by: java.lang.reflect.InvocationTargetException
03-07 05:16:33.318: E/AndroidRuntime(883): at google.map.activity.GoogleMapActivity.gpsToastButton(GoogleMapActivity.java:64)
03-07 05:16:33.318: E/AndroidRuntime(883): at java.lang.reflect.Method.invokeNative(Native Method)
03-07 05:16:33.318: E/AndroidRuntime(883): at java.lang.reflect.Method.invoke(Method.java:521)
03-07 05:16:33.318: E/AndroidRuntime(883): at android.view.View$1.onClick(View.java:2067)
03-07 05:16:33.318: E/AndroidRuntime(883): ... 11 more
03-07 05:16:33.318: E/AndroidRuntime(883): Caused by: java.io.IOException: Service not Available
03-07 05:16:33.318: E/AndroidRuntime(883): at android.location.Geocoder.getFromLocation(Geocoder.java:117)
03-07 05:16:33.318: E/AndroidRuntime(883): ... 15 more
提前谢谢。
// 编辑:好的我现在解决了。我将IOException抛出到gpsToastButton类并在最后一个else语句之后捕获它。谢谢你的帮助!
答案 0 :(得分:0)