android - LocationManager is null after initialization -


I have tried to initialize LocationManager and the button To get access to the location obtained on the screen as a toast , although I know that the location manager is absent:

/ MyLocationTest: Failed to start the location manager < / p>

  on android.view.View $ PerformClick.run on com.example.mylocationtest.MainActivity $ 1.onClick (MainActivity.java:46) android.view.View.performClick (View.java: 4240) on java.lang.NullPointerException (View.java:17721) android.os.Handler.handleCallb ack (Handler.java:730) android.os.Handler.dispatchMessage (Handler.java:92) On android.os.Looper.loop on java.lang.reflect.Method.invoke (Looper.java:137) android .app.ActivityThread.main (ActivityThread.java TT103) at java.lang.reflect.Method.invokeNative (Native resident method) (Method.javaitter25) at com. android.internal.os.ZygoteInit dalvik.system.NativeStart.main (Native Method $ MethodAn dArgsCaller.run (ZygoteInit.java:737) com.android.internal.os.ZygoteInit.main (ZygoteInit.java:553)) The first line is a message that I print on  logkat  because  LocationManager  is zero, though I call it  Despite initializing in onCreate  
  locMan = (LocationManager) this.getSystemService (context.LOCATION_SERVICE); A click listener to set the // button: btn_loc.setOnClickListener (New View.OnClickListener () {@Override public void onClick (View v) {boolean isGPSReady, isNetworkReady, if (Locman == NULL) {Log.d ( "MyLocationTest "" failed to start service manager ");} isGPSReady = locMan.isProviderEnabled (LocationManager.GPS_PROVIDER); isNetworkReady = locMan.isProviderEnabled (LocationManager.NETWORK_PROVIDER); if (! Hanetvrkardii and amp ;! IsGPSReady) {toast. make text (GetBaseContext) (), "Sorry, location can not be enabled", Toast. LNGHHAR). Show ();} else if (isNetworkReady) {// The third parameter is a looper that control the callback where ... locMan.requestSingleUpdate (LocationManager.NETWORK_PROVIDER, locEar_network, null); Latitude = loc.getLatitude (); Longitude = loc.getLongitude (); Toast.makeText (getBaseContext), "Latitude is" + l Atitude + "and longitude" + Lat + + "and accuracy" + loc.getAccuracy (), Toast.LnnGHIHORAT). Show (); } And if (isGPSReady) {locMan.requestSingleUpdate (LocationManager.GPS_PROVIDER, locEar_gps, null); Latitude = loc.getLatitude (); Longitude = loc.getLongitude (); Toast. Make text (getBaseContext ()), "latitude" + latitude + "and longitude" + longitude + "and accuracy" + loc.getAccuracy (), toast. LNnGHIHORAR). Show (); }}});  

In my manifest, I used the following to ensure that API level 9 is minSDKVersion for requestSingleUpdate and the app has access to the correct location Allowed:

  & lt; Manifest xmlns: Android = "http://schemas.android.com/apk/res/android" package = "com.example.mylocationtest" & gt; M & lt; Use-sdk android: MinSdkVersion = "9" Android: targetSdkVersion = "18" /> & Lt; Usage-permission Android: name = "android.permission.ACCESS_FINE_LOCATION" /> Edit  

:

How can I ensure that the location manager is never weak?

If the location manager tries to obtain a location from the network provider, then perhaps someone with a network provider The location may not be available or may require some time.


Comments