Reputation: 6622
I have tried to initialize the LocationManager
and use a Button
to get the location obtained as a Toast
on the screen,however I find that the LocationManager is null:
/MyLocationTest﹕ Failed to initialize LocationManager
java.lang.NullPointerException
at com.example.mylocationtest.MainActivity$1.onClick(MainActivity.java:46)
at android.view.View.performClick(View.java:4240)
at android.view.View$PerformClick.run(View.java:17721)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
The first line is a message that I print to Logcat
because the LocationManager
is null even though I initialize it like this in onCreate
:
locMan=(LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
//set a click listener for 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 initialize LocationManager");
}
isGPSReady=locMan.isProviderEnabled(LocationManager.GPS_PROVIDER);
isNetworkReady=locMan.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if(!isNetworkReady && !isGPSReady)
{
Toast.makeText(getBaseContext(),"Sorry,location could not be enabled",Toast.LENGTH_SHORT).show();
}
else if(isNetworkReady)
{
//the third parameter is a looper that controls where the callback happens from...
locMan.requestSingleUpdate(LocationManager.NETWORK_PROVIDER,locEar_network, null);
latitude=loc.getLatitude();
longitude=loc.getLongitude();
Toast.makeText(getBaseContext(),"The latitude is "+latitude+" and the longitude is "+longitude+" and the accuracy is "+loc.getAccuracy(),Toast.LENGTH_SHORT).show();
}
else if(isGPSReady)
{
locMan.requestSingleUpdate(LocationManager.GPS_PROVIDER,locEar_gps,null);
latitude=loc.getLatitude();
longitude=loc.getLongitude();
Toast.makeText(getBaseContext(),"The latitude is "+latitude+" and the longitude is "+longitude+ "and the accuracy is "+loc.getAccuracy(),Toast.LENGTH_SHORT).show();
}
}
});
In my manifest,I have used the following to ensure that API level 9 is the minSDKVersion for requestSingleUpdate
and the app is permitted access to fine location:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mylocationtest" >m
<uses-sdk android:minSdkVersion="9"
android:targetSdkVersion="18"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
The entire class is here as a pastebin
How can I ensure that the LocationManager is never null?
Upvotes: 2
Views: 2352
Reputation: 4503
You are only setting up a log if the LocationManager is null. You should avoid accessing it when it's null also. Modify you code like this.
boolean isGPSReady,isNetworkReady;
if(locMan==null)
{
Log.d("MyLocationTest", "Failed to initialize LocationManager");
}
else{
isGPSReady=locMan.isProviderEnabled(LocationManager.GPS_PROVIDER);
isNetworkReady=locMan.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if(!isNetworkReady && !isGPSReady)
{
Toast.makeText(getBaseContext(),"Sorry,location could not be enabled",Toast.LENGTH_SHORT).show();
}
else if(isNetworkReady)
{
//the third parameter is a looper that controls where the callback happens from...
locMan.requestSingleUpdate(LocationManager.NETWORK_PROVIDER,locEar_network, null);
latitude=loc.getLatitude();
longitude=loc.getLongitude();
Toast.makeText(getBaseContext(),"The latitude is "+latitude+" and the longitude is "+longitude+" and the accuracy is "+loc.getAccuracy(),Toast.LENGTH_SHORT).show();
}
else if(isGPSReady)
{
locMan.requestSingleUpdate(LocationManager.GPS_PROVIDER,locEar_gps,null);
latitude=loc.getLatitude();
longitude=loc.getLongitude();
Toast.makeText(getBaseContext(),"The latitude is "+latitude+" and the longitude is "+longitude+ "and the accuracy is "+loc.getAccuracy(),Toast.LENGTH_SHORT).show();
}
}
One more thing, are you using a GPS enabled device to test this? If you want more information refer this.
Try adding these two permissions also to AndroidManifest.xml file.
INTERNET
ACCESS_COARSE_LOCATION
Upvotes: 1
Reputation: 2882
If Location manager tries to get a location from the network provider, some times there might not be a location available from the network provider or it may require some time to get the location in these cases you might get a null.
Location manager might also get null if the gps coudnt find any location most probably inside a building
Upvotes: 1