0
votes

I want to calculate distance between two point but when i use .distanceto function the program has error and say android.location.Location conflicts with a type defined in the same file. I calculate distance by mathematical rules but when i give latitude and longitude of my location , when the program runs, the program has stopped. Why? I'm confused.

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

THanks. Cheers

2
Re you running n the emulator?smk
Hey, I think you should change name of your class first. Make it "MyLocation" or something else, as Location is already in Android API.MysticMagicϡ
I run the program on the mobile phone.user1797707

2 Answers

1
votes

Your location is NULL use below code

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

There is already a class called Location.

Renaming it to MyLocation or something should solve this problem.