0

我正在开发一个应用程序,用户需要提供他们的纬度和经度位置才能发布任何信息。问题是当我使用以下代码时:

/********************************************************************
* 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;
}

我看到以下错误消息:

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

我使用以下权限来使用 LocationManager:

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

还有图书馆:

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

有人可以告诉我确切的问题是什么吗?是什么导致 java.lang.reflect.invocationTargetException?我不明白。谢谢!

4

2 回答 2

2

lastKnownLocation即使其他一切正常,也可以为空。就像这里的情况一样: Android LocationManager.getLastKnownLocation() 返回 null

如果 GPS 正在等待修复,并且没有有效lastKnownLocation的,返回null实际上是非常明智的。您必须等待修复。

于 2012-12-06T21:42:56.470 回答
2

lastKnownLocation 可以返回 null。

gps 使用观察者设计模式工作 - 您请求它给您一个位置,并使用侦听器,它会自己调用相关函数,给您它找到的位置。

于 2012-12-06T21:47:45.117 回答