我无法访问我所在位置的纬度和经度。为什么?

时间:2012-11-12 12:16:45

标签: android map error-handling

我想计算两点之间的距离,但是当我使用.distanceto函数时,程序有错误,并说android.location.Location与同一文件中定义的类型冲突。我通过数学规则计算距离但是当我给出我的位置的纬度和经度时,程序运行时,程序已经停止。为什么?我很困惑。

public class Location extends MapActivity {
double myLatitude=0;
double myLongitude=0;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.location);

    initMapView();
    initMyLocation();
    }
    private void initMyLocation() {
    final MyLocationOverlay overlay = new MyLocationOverlay(this, map);
    overlay.enableMyLocation();
    overlay.enableCompass(); 
    //
    mylocation=overlay.getMyLocation();
    overlay.runOnFirstFix(new Runnable() {
        public void run() {
            controller.setZoom(13);
            controller.animateTo(overlay.getMyLocation());
        }
    });

            //THIS is ERROR . WHY?
    myLatitude=(overlay.getMyLocation().getLatitudeE6())/1e6;
    myLongitude=(overlay.getMyLocation().getLongitudeE6())/1e6;
}
}

logcat的:

11-12 15:29:57.974: W/dalvikvm(688): threadid=1: thread exiting with uncaught exception (group=0x40a891f8)
11-12 15:29:57.982: E/AndroidRuntime(688): FATAL EXCEPTION: main
11-12 15:29:57.982: E/AndroidRuntime(688): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.example.loyaltier/org.example.loyaltier.Location}: java.lang.NullPointerException
11-12 15:29:57.982: E/AndroidRuntime(688):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
11-12 15:29:57.982: E/AndroidRuntime(688):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
11-12 15:29:57.982: E/AndroidRuntime(688):  at android.app.ActivityThread.access$600(ActivityThread.java:122)
11-12 15:29:57.982: E/AndroidRuntime(688):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
11-12 15:29:57.982: E/AndroidRuntime(688):  at android.os.Handler.dispatchMessage(Handler.java:99)
11-12 15:29:57.982: E/AndroidRuntime(688):  at android.os.Looper.loop(Looper.java:137)
11-12 15:29:57.982: E/AndroidRuntime(688):  at android.app.ActivityThread.main(ActivityThread.java:4340)
11-12 15:29:57.982: E/AndroidRuntime(688):  at java.lang.reflect.Method.invokeNative(Native Method)
11-12 15:29:57.982: E/AndroidRuntime(688):  at java.lang.reflect.Method.invoke(Method.java:511)
11-12 15:29:57.982: E/AndroidRuntime(688):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
11-12 15:29:57.982: E/AndroidRuntime(688):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
11-12 15:29:57.982: E/AndroidRuntime(688):  at dalvik.system.NativeStart.main(Native Method)
11-12 15:29:57.982: E/AndroidRuntime(688): Caused by: java.lang.NullPointerException
11-12 15:29:57.982: E/AndroidRuntime(688):  at org.example.loyaltier.Location.initMyLocation(Location.java:164)
11-12 15:29:57.982: E/AndroidRuntime(688):  at org.example.loyaltier.Location.onCreate(Location.java:61)
11-12 15:29:57.982: E/AndroidRuntime(688):  at android.app.Activity.performCreate(Activity.java:4465)
11-12 15:29:57.982: E/AndroidRuntime(688):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
11-12 15:29:57.982: E/AndroidRuntime(688):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
11-12 15:29:57.982: E/AndroidRuntime(688):  ... 11 more

感谢。干杯

2 个答案:

答案 0 :(得分:1)

您的位置为空使用代码

 if(mylocation!= null) 
{
      myLatitude=(overlay.getMyLocation().getLatitudeE6())/1e6;
    myLongitude=(overlay.getMyLocation().getLongitudeE6())/1e6;
}

答案 1 :(得分:0)

已经有一个名为Location的课程。

将其重命名为MyLocation或其他东西可以解决此问题。