2

我遇到了一些 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
4

3 回答 3

3

你没有初始化endLoc你只是将它声明为一个变量。

在您的构造函数中,或者在调用第 171 行之前的某个方法中的某个地方,您需要endLoc = new Location();

于 2012-05-02T11:59:56.383 回答
0

位置构造函数需要提供者字符串,但我在某处看到它可以是空字符串

loc = new Location("");

一会儿测试一下;

于 2012-08-26T14:31:26.293 回答
0

您只声明endLoc了 ,但此时不会分配内存。您需要通过调用来初始化内存:

endLoc = new Location(....)
于 2012-05-02T12:04:53.093 回答