我的应用程序遇到问题,找不到用户位置并将其显示在屏幕上。在调试时,应用程序只会在模拟器和手机上崩溃。
logcat给了我这个:
03-26 15:41:37.958: ERROR/AndroidRuntime(871): Uncaught handler: thread main exiting due to uncaught exception
03-26 15:41:37.988: ERROR/AndroidRuntime(871): java.lang.NullPointerException
03-26 15:41:37.988: ERROR/AndroidRuntime(871): at com.finalyear.fitnessapp.fitnessMapActivity$GeoUpdateHandler.onLocationChanged(fitnessMapActivity.java:62)
03-26 15:41:37.988: ERROR/AndroidRuntime(871): at android.location.LocationManager$ListenerTransport._handleMessage(LocationManager.java:179)
03-26 15:41:37.988: ERROR/AndroidRuntime(871): at android.location.LocationManager$ListenerTransport.access$000(LocationManager.java:112)
03-26 15:41:37.988: ERROR/AndroidRuntime(871): at android.location.LocationManager$ListenerTransport$1.handleMessage(LocationManager.java:128)
03-26 15:41:37.988: ERROR/AndroidRuntime(871): at android.os.Handler.dispatchMessage(Handler.java:99)
03-26 15:41:37.988: ERROR/AndroidRuntime(871): at android.os.Looper.loop(Looper.java:123)
03-26 15:41:37.988: ERROR/AndroidRuntime(871): at android.app.ActivityThread.main(ActivityThread.java:4603)
03-26 15:41:37.988: ERROR/AndroidRuntime(871): at java.lang.reflect.Method.invokeNative(Native Method)
03-26 15:41:37.988: ERROR/AndroidRuntime(871): at java.lang.reflect.Method.invoke(Method.java:521)
03-26 15:41:37.988: ERROR/AndroidRuntime(871): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
03-26 15:41:37.988: ERROR/AndroidRuntime(871): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
03-26 15:41:37.988: ERROR/AndroidRuntime(871): at dalvik.system.NativeStart.main(Native Method)
03-26 15:41:40.018: ERROR/ActivityManager(87): fail to set top app changed!
03-26 15:41:51.238: ERROR/MapActivity(933): Couldn't get connection factory client
我的代码是:
public class GeoUpdateHandler implements LocationListener {
@Override
public void onLocationChanged(Location location) {
int lat = (int) (location.getLatitude() * 1E6);
int lng = (int) (location.getLongitude() * 1E6);
GeoPoint point = new GeoPoint(lat, lng);
mapController.animateTo(point);
mapController.setCenter(point);
}
任何帮助都会被困在这几周了。
答案 0 :(得分:3)
答案 1 :(得分:1)
如果您还没有套装,则需要在清单文件中设置GPS位置的使用权限。
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
此外,如果您有任何机会如上所述出现NullPointerException,则应始终检查传递的空值。在这种情况下,如果您尝试检索GPS坐标并且没有权限
,则应检查位置,因为位置可以为null