开发者

Nullpointer exception with location manager

I wrote the below code for getting lat long positions

 myLocationManager = (LocationManager)getSystemService    
  (Context.LOCATION_SERVICE);
                  myLocationListener = new MyLocationListener();

          myLocationManager.requestLocationUpdates
  (LocationManager.GPS_PROVIDER, 1, 1, myLocationListener);

  //HERE UNCOMMENT
   latitude=myLocationManager.getLastKnownLocation
  (LocationManager.GPS_PROVIDER).getLatitude();
   longitude=myLocationManager.getLastKnownLocation
         (LocationManager.GPS_PROVIDER).getLongitude();

I am getting Nullpointer exception at the follow开发者_如何学Cing line

(LocationManager.GPS_PROVIDER).getLatitude();

I am testing the code on my Android 2.1 phone with GPS enabled and I gave the following permissions in the manifest file

android.permission.ACCESS_FINE_LOCATION  
android.permission.ACCESS_COARSE_LOCATION  
android.permission.INTERNET  
android.permission.ACCESS_WIFI_STATE  
android.permission.ACCESS_NETWORK_STATE  
android.permission.VIBRATE  


Did you send coordinates from DDMS? If not. Do it then envoke request for location updates, I'm sure that 99.9% you wont get NPE again for this.


There is no guarantee that you have a last known location. Therefore, it can come back as null.


If you are working with emulator then without fixing the coordinate you will get null pointer exception.To fix in emulator

goto command prompt:

type: telnet localhost 5554 then press enter and then type

geo fix lat lon enter code hereenter and now you will get lat lon

Here 5554 is your emulator serial number

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜