iHank
iHank

Reputation: 536

getSystemService(Context.Location_SERVICE) causes java.lang.NullPointerException

I'm currently creating an application with Google maps using MapFragment. In order to give the user a possibility to see location changes, I'm trying to create a LocationManager-object in the onCreate-method of the MapFragment. The problem is that the app crashes and it's caused by java.lang.NullPointerException. My initialization look like this:

locationManager = (LocationManager)parent.getSystemService(Context.LOCATION_SERVICE);

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);

-parent is the actitivy from which the MapFragment is executing.

My permissions look like this:

 <permission
        android:name="com.example.tipspromenad.MAPS_RECEIVE"
        android:protectionLevel="signature" />

    <uses-permission android:name="com.example.tipspromenad.MAPS_RECEIVE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />

What can be wrong?

Hank

Upvotes: 1

Views: 4772

Answers (1)

M D
M D

Reputation: 47817

I guess your parent is NULL at position so you got NPE

locationManager = (LocationManager)parent.getSystemService(Context.LOCATION_SERVICE);

Try this way

locationManager = (LocationManager)getActivity().getSystemService(Context.LOCATION_SERVICE);

in Fragment

Upvotes: 7

Related Questions