我遇到一些Android代码问题。在搜索了将GeoPoint转换为Location的方法之后,我发现this问题似乎给了我答案但是当我使用该信息时,我的程序失败并出现空指针异常。
我的代码:
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
答案 0 :(得分:3)
您没有初始化endLoc
您只是将其声明为变量。
在你的构造函数中,或者在调用第171行之前的某个方法中,你需要endLoc = new Location();
答案 1 :(得分:0)
您只声明了endLoc
,但此时不会分配内存。您需要通过调用:
endLoc = new Location(....)
答案 2 :(得分:0)
位置构造函数需要提供程序字符串,但我在某处看到它可以是空字符串
loc = new Location("");
片刻测试一下;