我对一些android代码有问题。在寻找将aGeoPoint转换为aLocation的方法之后,我发现了一个问题,这个问题似乎给了我答案,但是当我使用信息时,我的程序失败了,出现了空指针异常。
我的代码:

    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

最佳答案

你没有初始化endLoc你只是把它声明为一个变量。
在构造函数中,或者在调用第171行之前的某个方法中,需要endLoc = new Location();

10-07 13:43