Question

Summary

I AM RUNNING THIS CODE ON A REAL DEVICE, NOT AN EMULATOR.

I've been going through the Xamarin Monodroid Location Services tutorial - HERE

When I compile the code I get a "Java.Lang.IllegalArgumentException" at the following line of code -

protected override void OnResume()
        {
            base.OnResume();
            _locationManager.RequestLocationUpdates(_locationProvider, 0, 0, this);
            Log.Debug(LogTag, "Listening for location updates using " + _locationProvider + ".");
        }

Drilling down into the exception reveals the following -

Java.Lang.IllegalArgumentException: provider=
  at Android.Runtime.JNIEnv.CallVoidMethod (intptr,intptr,Android.Runtime.JValue[]) [0x00023] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.8.2-branch/a25a31d0/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.g.cs:368
  at Android.Locations.LocationManager.RequestLocationUpdates (string,long,single,Android.Locations.ILocationListener) [0x0004a] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.8.2-branch/a25a31d0/source/monodroid/src/Mono.Android/platforms/android-10/src/generated/Android.Locations.LocationManager.cs:814
  at com.xamarin.recipes.getlocation.Activity1.OnResume () [0x0001b] in /Users/SimonGilbert/Downloads/GetLocation/Activity1.cs:86
  at Android.App.Activity.n_OnResume (intptr,intptr) [0x00009] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.8.2-branch/a25a31d0/source/monodroid/src/Mono.Android/platforms/android-10/src/generated/Android.App.Activity.cs:2596
  at at (wrapper dynamic-method) object.383877df-2ff3-46a8-906d-6d9bd2915e69 (intptr,intptr) <IL 0x00011, 0x0001b>
  at 
  at --- End of managed exception stack trace ---
  at java.lang.IllegalArgumentException: provider=
  at    at android.os.Parcel.readException(Parcel.java:1331)
  at    at android.os.Parcel.readException(Parcel.java:1281)
  at    at android.location.ILocationManager$Stub$Proxy.requestLocationUpdates(ILocationManager.java:646)
  at    at android.location.LocationManager._requestLocationUpdates(LocationManager.java:614)
  at    at android.location.LocationManager.requestLocationUpdates(LocationManager.java:478)
  at    at com.xamarin.recipes.getlocation.Activity1.n_onResume(Native Method)
  at    at com.xamarin.recipes.getlocation.Activity1.onResume(Activity1.java:43)
  at    at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1154)
  at    at android.app.Activity.performResume(Activity.java:4540)
  at    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2437)
  at    at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2475)
  at    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1989)
  at    at android.app.ActivityThread.access$600(ActivityThread.java:124)
  at    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1148)
  at    at android.os.Handler.dispatchMessage(Handler.java:99)
  at    at android.os.Looper.loop(Looper.java:137)
  at    at android.app.ActivityThread.main(ActivityThread.java:4436)
  at    at java.lang.reflect.Method.invokeNative(Native Method)
  at    at java.lang.reflect.Method.invoke(Method.java:511)
  at    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
  at    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
  at    at dalvik.system.NativeStart.main(Native Method)
  at 

Question

  1. Has anyone seen this before?
  2. Any ideas as to what it might be that is causing it?

UPDATE

  • From reading around, it seems like adding the following may solve my problem, although I haven't had a chance to test it yet -

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    
  • It seems another solution may be to check that the network provider is available also -

    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 300000, 500, locationListener);
    
  • And perhaps to also check that the GPS Provider is available too -

    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 300000, 500, locationListener);    
    

Many thanks.

Was it helpful?

Solution

Passing in an empty criteria set worked, along with setting the permission in the AssemblyInfo file.

LocationManager mgr = (LocationManager) GetSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
String best = mgr.GetBestProvider(criteria, true);
//since you are using true as the second parameter, you will only get the best of providers which are enabled.
Location location = mgr.GetLastKnownLocation(best);

<uses-permission android:name="android.permission.NETWORK" />

OTHER TIPS

I have done in some different way, in OnCreate

    btnDone = FindViewById<Button> (Resource.Id.btnDone);
        LocationManager LC=(LocationManager) GetSystemService(Context.LocationService);
        Criteria criteria=new Criteria();
        string  provider=LC.GetBestProvider(criteria, true);
        Location loc=LC.GetLastKnownLocation(provider);
`btnDone.Click += delegate {
OnLocationChanged(loc);
}

// outside OnCreate
public void OnLocationChanged(Location location) {

        double  lattitude = location.Latitude;
        double longitude = location.Longitude;
        Toast.MakeText (this, "Lattitude and Longitude is "+lattitude.ToString()+" and "+longitude.ToString(), ToastLength.Long).Show ();
    }

also Implement ILocationListener. refer this http://docs.xamarin.com/recipes/android/os_device_resources/gps/get_current_device_location/``

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top