0

I'm working on an app where users need to give their latitude and longitude location in order to post any information. The problem is when I use the following code:

/********************************************************************
* GET LATITUDE                                                      *
********************************************************************/
public String getLatitude(){
    LocationManager lm = (LocationManager) ACTIVITY.getSystemService(Context.LOCATION_SERVICE); 

    if( lm.isProviderEnabled( LocationManager.GPS_PROVIDER )){
        Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        if(location != null){
            return Double.toString( location.getLatitude() );
        }
    }
    return null;
}


/********************************************************************
* GET LONGITUDE                                                     *
********************************************************************/
public String getLongitude(){
    LocationManager lm = (LocationManager) ACTIVITY.getSystemService(Context.LOCATION_SERVICE); 

    if( lm.isProviderEnabled( LocationManager.GPS_PROVIDER )){
        Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        if(location != null){
            return Double.toString( location.getLongitude() );
        }
    }
    return null;
}

I get to see the following error messages:

12-06 22:31:46.791: W/dalvikvm(1173): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
12-06 22:31:46.811: E/AndroidRuntime(1173): FATAL EXCEPTION: main
12-06 22:31:46.811: E/AndroidRuntime(1173): java.lang.IllegalStateException: Could not execute method of the activity
12-06 22:31:46.811: E/AndroidRuntime(1173):     at android.view.View$1.onClick(View.java:3044)
12-06 22:31:46.811: E/AndroidRuntime(1173):     at android.view.View.performClick(View.java:3511)
12-06 22:31:46.811: E/AndroidRuntime(1173):     at android.view.View$PerformClick.run(View.java:14105)
12-06 22:31:46.811: E/AndroidRuntime(1173):     at android.os.Handler.handleCallback(Handler.java:605)
12-06 22:31:46.811: E/AndroidRuntime(1173):     at android.os.Handler.dispatchMessage(Handler.java:92)
12-06 22:31:46.811: E/AndroidRuntime(1173):     at android.os.Looper.loop(Looper.java:137)
12-06 22:31:46.811: E/AndroidRuntime(1173):     at android.app.ActivityThread.main(ActivityThread.java:4424)
12-06 22:31:46.811: E/AndroidRuntime(1173):     at java.lang.reflect.Method.invokeNative(Native Method)
12-06 22:31:46.811: E/AndroidRuntime(1173):     at java.lang.reflect.Method.invoke(Method.java:511)
12-06 22:31:46.811: E/AndroidRuntime(1173):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
12-06 22:31:46.811: E/AndroidRuntime(1173):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-06 22:31:46.811: E/AndroidRuntime(1173):     at dalvik.system.NativeStart.main(Native Method)
12-06 22:31:46.811: E/AndroidRuntime(1173): Caused by: java.lang.reflect.InvocationTargetException
12-06 22:31:46.811: E/AndroidRuntime(1173):     at java.lang.reflect.Method.invokeNative(Native Method)
12-06 22:31:46.811: E/AndroidRuntime(1173):     at java.lang.reflect.Method.invoke(Method.java:511)
12-06 22:31:46.811: E/AndroidRuntime(1173):     at android.view.View$1.onClick(View.java:3039)
12-06 22:31:46.811: E/AndroidRuntime(1173):     ... 11 more
12-06 22:31:46.811: E/AndroidRuntime(1173): Caused by: java.lang.NullPointerException
12-06 22:31:46.811: E/AndroidRuntime(1173):     at com.example.cay.saati.Helper.getLatitude(Helper.java:100)
12-06 22:31:46.811: E/AndroidRuntime(1173):     at com.example.cay.saati.MenuPage.prepareLoginInformation(MenuPage.java:230)
12-06 22:31:46.811: E/AndroidRuntime(1173):     at  com.example.cay.saati.MenuPage.onClick(MenuPage.java:267)
12-06 22:31:46.811: E/AndroidRuntime(1173):     ... 14 more

I use the following permissions to use LocationManager:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

And also library:

<uses-library android:name="com.google.android.maps" />

Can someone tell me what the exact problem is? What does cause java.lang.reflect.invocationTargetException? I don't get it. Thanks!

j0k
  • 22,600
  • 28
  • 79
  • 90
Tolga Demir
  • 161
  • 2
  • 11

2 Answers2

2

The lastKnownLocation can be null even when everything else is working. As was the case here: Android LocationManager.getLastKnownLocation() returns null

If the GPS is waiting to get a fix, and has no valid lastKnownLocation, returning null is actually quite sensible. You'll have to wait to get a fix.

Community
  • 1
  • 1
emrys57
  • 6,679
  • 3
  • 39
  • 49
  • 2
    @Xarialon Well you could check for null responses? – keyser Dec 06 '12 at 21:48
  • What can I do to get the coordinations? Is there a better method? – Tolga Demir Dec 06 '12 at 21:50
  • 1
    In general, getting a good location is actually quite hard. There are 175 lines of code in the one in front of me. Sorry, I'm not allowed to share it, it's copyrighted. Even then, until a fix is returned, you're not going to have an answer. You could try the cellphone locator service instead of GPS, that often has a quicker result, although not so precise. – emrys57 Dec 06 '12 at 21:55
2

lastKnownLocation can return null.

gps works using an observer design pattern - you request it to give you a location , and using a listener , it will call the relevant function by itself , giving you the location it has found .

android developer
  • 114,585
  • 152
  • 739
  • 1,270
  • Is there a better method to get the geo coordinates? – Tolga Demir Dec 06 '12 at 21:49
  • 1
    @Xarialon No. This is the method. You need to learn what a null pointer exception is then amend your code to check for, and deal with, the null return. This is normal coding. – Simon Dec 06 '12 at 21:54
  • I know the null pointer exception, but I want to get the coordinations when the gps is on. I will search for the observer design pattern because this is the first time I've heard of. Returning null is common, but I want to avoid that. – Tolga Demir Dec 06 '12 at 21:57
  • since it takes time for the gps to get the coordinate , you tell it to give it to you when it's ready . it could take a second , it could take a minute (for example if the user is driving through a tunnel) . anyway , the observer design pattern is used in many places . classes methods that use it usually have their names as "set...Listener" or "add...Listener" . for gps it's "requestLocationUpdates" . – android developer Dec 07 '12 at 19:43
  • Well, I think that this question was obvious, but I wanted to get some extra info because of the strange reason that I was confused by the null value. – Tolga Demir Jan 11 '13 at 09:47