我遇到了一些 Android 代码的问题。在寻找将GeoPoint转换为Location的方法后,我发现这个问题似乎给了我答案,但是当我使用这些信息时,我的程序因空指针异常而失败。
我的代码:
lat = 52.3725979;
longt = 4.8998594;
final GeoPoint geo_1 = new GeoPoint((int) (lat * 1E6),(int) (longt * 1E6));
double latitude = geo_1.getLatitudeE6() / 1E6;
double longitude = geo_1.getLongitudeE6() / 1E6;
endLoc.setLatitude(latitude);
endLoc.setLongitude(longitude);
第 171 行:
endLoc.setLatitude(latitude);
endLoc 变量的初始化:
public class VibroNavActivity extends Activity implements SensorListener{
final static String TAG = "VibroNavActivity";
//location variables
GeoPoint startGeo;
GeoPoint endGeo;
Location loc = null;
Location startLoc;
Location endLoc;
float totalDistance;//the distance between the startpoint end the endpoint
日志:
05-02 12:40:56.789: W/dalvikvm(27983): threadid=1: thread exiting with uncaught exception (group=0x40a4b1f8)
05-02 12:40:56.800: E/AndroidRuntime(27983): FATAL EXCEPTION: main
05-02 12:40:56.800: E/AndroidRuntime(27983): java.lang.RuntimeException: Unable to start activity ComponentInfo{hcm.haska/hcm.haska.VibroNavActivity}: java.lang.NullPointerException
05-02 12:40:56.800: E/AndroidRuntime(27983): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
05-02 12:40:56.800: E/AndroidRuntime(27983): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
05-02 12:40:56.800: E/AndroidRuntime(27983): at android.app.ActivityThread.access$600(ActivityThread.java:123)
05-02 12:40:56.800: E/AndroidRuntime(27983): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
05-02 12:40:56.800: E/AndroidRuntime(27983): at android.os.Handler.dispatchMessage(Handler.java:99)
05-02 12:40:56.800: E/AndroidRuntime(27983): at android.os.Looper.loop(Looper.java:137)
05-02 12:40:56.800: E/AndroidRuntime(27983): at android.app.ActivityThread.main(ActivityThread.java:4424)
05-02 12:40:56.800: E/AndroidRuntime(27983): at java.lang.reflect.Method.invokeNative(Native Method)
05-02 12:40:56.800: E/AndroidRuntime(27983): at java.lang.reflect.Method.invoke(Method.java:511)
05-02 12:40:56.800: E/AndroidRuntime(27983): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
05-02 12:40:56.800: E/AndroidRuntime(27983): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
05-02 12:40:56.800: E/AndroidRuntime(27983): at dalvik.system.NativeStart.main(Native Method)
05-02 12:40:56.800: E/AndroidRuntime(27983): Caused by: java.lang.NullPointerException
05-02 12:40:56.800: E/AndroidRuntime(27983): at hcm.haska.VibroNavActivity.onCreate(VibroNavActivity.java:171)
05-02 12:40:56.800: E/AndroidRuntime(27983): at android.app.Activity.performCreate(Activity.java:4465)
05-02 12:40:56.800: E/AndroidRuntime(27983): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
05-02 12:40:56.800: E/AndroidRuntime(27983): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
05-02 12:40:56.800: E/AndroidRuntime(27983): ... 11 more