Question

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!

Était-ce utile?

La solution

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 .

Autres conseils

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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top