0

I'm having some issues when I am running my method. How can I get it working so my mock locations will work on my actual device. (Ps. I've included permissions).

This provides a null pointer exception and I am not to sure why?

Method:

 private void setMockLocation() {
        {
            Context context = null;
            LocationManager locationManager = (LocationManager)context.getSystemService( Context.LOCATION_SERVICE );

            Criteria criteria = new Criteria();
            criteria.setAccuracy( Criteria.ACCURACY_COARSE );
            String provider = locationManager.getBestProvider( criteria, true );

            if ( provider == null ) {
                Log.e( TAG, "No location provider found!" );
                return;
            }

            locationManager.getLastKnownLocation(provider);

            Location newLocation = new Location(LocationManager.GPS_PROVIDER);

            newLocation.setLatitude(55.9500);
            newLocation.setLongitude(3.1833);

            newLocation.setAccuracy(500);

            mLocationManager.setTestProviderEnabled
                    (
                            LocationManager.GPS_PROVIDER,
                            true
                    );

            mLocationManager.setTestProviderStatus
                    (
                            LocationManager.GPS_PROVIDER,
                            LocationProvider.AVAILABLE,
                            null,
                            System.currentTimeMillis()
                    );

            mLocationManager.setTestProviderLocation
                    (
                            LocationManager.GPS_PROVIDER,
                            newLocation
                    );
        }
    }

Stack Trace:

05-10 18:50:46.218  14398-14398/com.example.ankhit.saveme E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.NullPointerException
            at com.example.ankhit.saveme.UserLocation.setMockLocation(UserLocation.java:220)
            at com.example.ankhit.saveme.UserLocation.access$000(UserLocation.java:42)
            at com.example.ankhit.saveme.UserLocation$4.onClick(UserLocation.java:175)
            at android.view.View.performClick(View.java:4439)
            at android.view.View$PerformClick.run(View.java:18398)
            at android.os.Handler.handleCallback(Handler.java:725)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:176)
            at android.app.ActivityThread.main(ActivityThread.java:5299)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
            at dalvik.system.NativeStart.main(Native Method)

However, when I add the code below to Context context code shown above I get a 'GPS Provider Unknown' Error

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

        Criteria criteria = new Criteria();
        criteria.setAccuracy( Criteria.ACCURACY_COARSE );
        String provider = locationManager.getBestProvider( criteria, true );

        if ( provider == null ) {
            Log.e( TAG, "No location provider found!" );
            return;
        }

        lastLocation = locationManager.getLastKnownLocation(provider);

StackTrace:

05-10 18:54:37.395  15159-15159/com.example.ankhit.saveme E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.IllegalArgumentException: Provider "gps" unknown
            at android.os.Parcel.readException(Parcel.java:1429)
            at android.os.Parcel.readException(Parcel.java:1379)
            at android.location.ILocationManager$Stub$Proxy.removeTestProvider(ILocationManager.java:956)
            at android.location.LocationManager.removeTestProvider(LocationManager.java:1194)
            at com.example.ankhit.saveme.UserLocation.setMockLocation(UserLocation.java:218)
            at com.example.ankhit.saveme.UserLocation.access$000(UserLocation.java:42)
            at com.example.ankhit.saveme.UserLocation$4.onClick(UserLocation.java:175)
            at android.view.View.performClick(View.java:4439)
            at android.view.View$PerformClick.run(View.java:18398)
            at android.os.Handler.handleCallback(Handler.java:725)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:176)
            at android.app.ActivityThread.main(ActivityThread.java:5299)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
            at dalvik.system.NativeStart.main(Native Method)

Any Ideas?

2
  • Can you please share your stacktrace?
    – Mureinik
    May 10, 2015 at 18:47
  • @Mureinik...See edits above.
    – TechCabana
    May 10, 2015 at 18:51

1 Answer 1

1

The NullPointerException should come from

 Context context = null;
 LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE );

because context is null and you invoke a method on it.

2
  • Hmm, but then how do I initialise it? If I do Context context; the context.getSystemService throws an error since context is never initialised? So how can I get it working?
    – TechCabana
    May 10, 2015 at 20:19
  • If you have this code in a View then Context context = this could work. I am really not sure about that, didn't do much with Android. But I think View implements Context. This might be completely wrong... I only know, that you can't do null.invokeThisMethod() ^^ May 10, 2015 at 20:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.