0

我想计算两点之间的距离,但是当我使用 .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;
}
}

日志猫:

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

谢谢。干杯

4

2 回答 2

1

您的位置为 NULL使用下面的代码

 if(mylocation!= null) 
{
      myLatitude=(overlay.getMyLocation().getLatitudeE6())/1e6;
    myLongitude=(overlay.getMyLocation().getLongitudeE6())/1e6;
}
于 2012-11-12T12:20:48.960 回答
0

已经有一个名为 Location 的类。

将其重命名为MyLocation或其他内容应该可以解决此问题。

于 2012-11-12T12:34:24.557 回答