Xamarin.Forms LocationManager error on RequestLocationUpdates

Hello everyone.
I am developing an app with Xamarin.Forms that allows me to know the time by taking it from the GPS.
To do this I'm taking advantage of the LocationManager class in the MainActivity but unfortunately just launch the RequestLocationUpdates method the app crashes.
The detail of the crash is as follows

12-18 16:30:50.837 D/Mono (25728): Assembly Ref addref Xamarin.Android.Support.Fragment[0xadccd700] -> Xamarin.Android.Support.Media.Compat[0xadccd7c0]: 2
12-18 16:30:51.087 D/Mono (25728): Assembly Ref addref Xamarin.Android.Support.Fragment[0xadccd700] -> Xamarin.Android.Support.Core.Utils[0xadccd580]: 3
12-18 16:31:20.927 E/art (25728): JNI ERROR (app bug): attempt to pass an instance of md52f4761d89c43914387d849b16f8eb064.MainActivity as argument 4 to void android.location.LocationManager.requestLocationUpdates(java.lang.String, long, float, android.location.LocationListener)
referenceTable GDEF length=814 1
referenceTable GSUB length=11364 1
referenceTable GPOS length=47302 1
12-18 16:31:20.977 F/art 12-18 16:31:20.977 F/art (25728): art/runtime/java_vm_ext.cc:410] JNI DETECTED ERROR IN APPLICATION: bad arguments passed to void android.location.LocationManager.requestLocationUpdates(java.lang.String, long, float, android.location.LocationListener) (see above for details)
12-18 16:31:20.977 F/art (25728): art/runtime/java_vm_ext.cc:410] from void md52f4761d89c43914387d849b16f8eb064.MainActivity.n_onCreate(android.os.Bundle)
12-18 16:31:20.977 F/art (25728): art/runtime/java_vm_ext.cc:410] "main" prio=5 tid=1 Runnable
12-18 16:31:20.977 F/art (25728): art/runtime/java_vm_ext.cc:410] | group="main" sCount=0 dsCount=0 obj=0x76537598 self=0xb4864500
12-18 16:31:20.977 F/art (25728): art/runtime/java_vm_ext.cc:410] | sysTid=25728 nice=-11 cgrp=apps sched=0/0 handle=0xb6fe5b4c
12-18 16:31:20.977 F/art (25728): art/runtime/java_vm_ext.cc:410] | state=R schedstat=( 3438601580 197147438 1598 ) utm=265 stm=78 core=0 HZ=100
12-18 16:31:20.977 F/art (25728): art/runtime/java_vm_ext.cc:410] | stack=0xbe3f3000-0xbe3f5000 stackSize=8MB
12-18 16:31:20.977 F/art (25728): art/runtime/java_vm_ext.cc:410] | held mutexes= "mutator lock"(shared held)
12-18 16:31:20.977 F/art (25728): art/runtime/java_vm_ext.cc:410] native: #00 pc 00371871 /system/lib/libart.so (ZN3art15DumpNativeStackERNSt3__113basic_ostreamIcNS0_11char_traitsIcEEEEiPKcPNS_9ArtMethodEPv+160)
12-18 16:31:20.977 F/art (25728): art/runtime/java_vm_ext.cc:410] native: #01 pc 00350eeb /system/lib/libart.so (_ZNK3art6Thread4DumpERNSt3__113basic_ostreamIcNS1_11char_traitsIcEEEE+150)
12-18 16:31:20.977 F/art (25728): art/runtime/java_vm_ext.cc:410] native: #02 pc 0025aed5 /system/lib/libart.so (_ZN3art9JavaVMExt8JniAbortEPKcS2
+740)
12-18 16:31:20.977 F/art (25728): art/runtime/java_vm_ext.cc:410] native: #03 pc 0025b62b /system/lib/libart.so (_ZN3art9JavaVMExt9JniAbortFEPKcS2_z+74)
12-18 16:31:20.977 F/art (25728): art/runtime/java_vm_ext.cc:410] native: #04 pc 0032c5ab /system/lib/libart.so (_ZN3artL20CheckMethodArgumentsEPNS_9JavaVMExtEPNS_9ArtMethodEPj+1566)
12-18 16:31:20.977 F/art (25728): art/runtime/java_vm_ext.cc:410] native: #05 pc 0032d619 /system/lib/libart.so (_ZN3art35InvokeVirtualOrInterfaceWithJValuesERKNS_33ScopedObjectAccessAlreadyRunnableEP8_jobjectP10_jmethodIDP6jvalue+716)
12-18 16:31:20.977 F/art (25728): art/runtime/java_vm_ext.cc:410] native: #06 pc 00295701 /system/lib/libart.so (_ZN3art3JNI15CallVoidMethodAEP7_JNIEnvP8_jobjectP10_jmethodIDP6jvalue+344)
12-18 16:31:20.977 F/art (25728): art/runtime/java_vm_ext.cc:410] native: #07 pc 0010fadd /system/lib/libart.so (_ZN3art8CheckJNI11CallMethodAEPKcP7_JNIEnvP8_jobjectP7_jclassP10_jmethodIDP6jvalueNS_9Primitive4TypeENS_10InvokeTypeE+928)
12-18 16:31:20.977 F/art (25728): art/runtime/java_vm_ext.cc:410] native: #08 pc 00110569 /system/lib/libart.so (_ZN3art8CheckJNI15CallVoidMethodAEP7_JNIEnvP8_jobjectP10_jmethodIDP6jvalue+36)
12-18 16:31:20.977 F/art (25728): art/runtime/java_vm_ext.cc:410] native: #09 pc 00008b4f /data/app/com.companyname.BluetoothTest-1/lib/arm/libmonodroid.so (java_interop_jnienv_call_void_method_a+28)
12-18 16:31:20.977 F/art (25728): art/runtime/java_vm_ext.cc:410] native: #10 pc 0002bef8 (???)
12-18 16:31:20.977 F/art (25728): art/runtime/java_vm_ext.cc:410] at md52f4761d89c43914387d849b16f8eb064.MainActivity.n_onCreate(Native method)
12-18 16:31:20.977 F/art (25728): art/runtime/java_vm_ext.cc:410] at md52f4761d89c43914387d849b16f8eb064.MainActivity.onCreate(MainActivity.java:30)
12-18 16:31:20.977 F/art (25728): art/runtime/java_vm_ext.cc:410] at android.app.Activity.performCreate(Activity.java:6876)
12-18 16:31:20.977 F/art (25728): art/runtime/java_vm_ext.cc:410] at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1135)
12-18 16:31:20.977 F/art (25728): art/runtime/java_vm_ext.cc:410] at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3206)
12-18 16:31:20.977 F/art (25728): art/runtime/java_vm_ext.cc:410] at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349)
12-18 16:31:20.977 F/art (25728): art/runtime/java_vm_ext.cc:410] at android.app.ActivityThread.access$1100(ActivityThread.java:221)
12-18 16:31:20.977 F/art (25728): art/runtime/java_vm_ext.cc:410] at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
12-18 16:31:20.977 F/art (25728): art/runtime/java_vm_ext.cc:410] at android.os.Handler.dispatchMessage(Handler.java:102)
12-18 16:31:20.977 F/art (25728): art/runtime/java_vm_ext.cc:410] at android.os.Looper.loop(Looper.java:158)
12-18 16:31:20.977 F/art (25728): art/runtime/java_vm_ext.cc:410] at android.app.ActivityThread.main(ActivityThread.java:7225)
12-18 16:31:20.977 F/art (25728): art/runtime/java_vm_ext.cc:410] at java.lang.reflect.Method.invoke!(Native method)
12-18 16:31:20.977 F/art (25728): art/runtime/java_vm_ext.cc:410] at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
12-18 16:31:20.977 F/art (25728): art/runtime/java_vm_ext.cc:410] at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
12-18 16:31:20.977 F/art (25728): art/runtime/java_vm_ext.cc:410]
12-18 16:31:20.977 F/art (25728): art/runtime/runtime.cc:368] Runtime aborting...
12-18 16:31:20.977 F/art (25728): art/runtime/runtime.cc:368]
12-18 16:31:20.977 F/libc (25728): Fatal signal 6 (SIGABRT), code -6 in tid 25728 (e.BluetoothTest)

This is the code I use:

`using System;
using Android.App;
using Android.Content.PM;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using Android.Bluetooth;
using Android.Content;
using Android.Locations;
using System.Collections.Generic;
using System.Linq;
using BluetoothTest.Droid;

[assembly: Xamarin.Forms.Dependency(typeof(MainActivity))]
namespace BluetoothTest.Droid
{

[Activity(Label = "BluetoothTest", Icon = "@drawable/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity, IOra, ILocationListener
{
    LocationManager _locationManager;
    Location _currentLocation;
    string _locationProvider;

    public DateTime OraGPS;

    protected override void OnCreate(Bundle bundle)
    {
        TabLayoutResource = Resource.Layout.Tabbar;
        ToolbarResource = Resource.Layout.Toolbar;

        base.OnCreate(bundle);

        global::Xamarin.Forms.Forms.Init(this, bundle);
        LoadApplication(new App());

        Bth.Receiver ricevitore = new Bth.Receiver(this);
        var filter = new IntentFilter(BluetoothDevice.ActionFound);
        RegisterReceiver(ricevitore, filter);

        InitializeLocationManager();

    }

    protected override void OnResume()
    {
        base.OnResume();

        _locationManager.RequestLocationUpdates(_locationProvider, 0, 0, this);
    }

    public void OnLocationChanged(Location location)
    {
        _currentLocation = location;
        if (_currentLocation != null)
        {
            OraGPS = new DateTime(location.Time, DateTimeKind.Utc);
        }
    }

    public string GetOra()
    { 
        string ora = OraGPS.ToString();

        return ora;
    }

    void InitializeLocationManager()
    {
        _locationManager = (LocationManager)GetSystemService(LocationService);


            Criteria criteriaForLocationService = new Criteria
            {
                Accuracy = Accuracy.Fine
            };

            IList<string> acceptableLocationProviders = _locationManager.GetProviders(criteriaForLocationService, true);

            if (acceptableLocationProviders.Any())
            {

            _locationProvider = acceptableLocationProviders.First();



            _locationManager.RequestLocationUpdates("gps", 5000, 10, this);

            }
            else
            {
                _locationProvider = string.Empty;
            }


    }

    public void OnProviderDisabled(string provider) { }

    public void OnProviderEnabled(string provider) { }

    public void OnStatusChanged(string provider, Availability status, Bundle extras) { }
}

}`

but at line
_locationManager.RequestLocationUpdates("gps", 5000, 10, this);

App crashes....
Where is problem???
I think the problem refers to the parameters passed to the method and in particular to the parameter "this" because it does not see it with LocationListener.

how could I solve?
Could you give me some indication?
My need is to get the date and time from the GPS regardless of the one set on the device when there is no internet connection.

I thank you so much for your help.

Posts

  • sls1jsls1j Member

    I'm having the exact same issue, though my code is less involved:

    [Activity(Label = "GPSStationAndroid", MainLauncher = true)]
    public class MainActivity : Activity, ILocationListener
    ....
    string provider = LocationManager.GpsProvider;
    if ( _locMgr.IsProviderEnabled(provider)){
    _locMgr.RequestLocationUpdates(provider, 0, 0, this);
    }

    It gives the same error complaining about the "this" parameter. Is there any help out there? Is this a Xamarin bug?

    JNI ERROR (app bug): attempt to pass an instance of md5abdca0891596bcd17a2cc152ab1032bb.MainActivity as argument 4 to void android.location.LocationManager.requestLocationUpdates(java.lang.String, long, float, android.location.LocationListener)
    12-20 09:18:46.245 E/art ( 7690): JNI DETECTED ERROR IN APPLICATION: bad arguments passed to void android.location.LocationManager.requestLocationUpdates(java.lang.String, long, float, android.location.LocationListener) (see above for details)
    12-20 09:18:46.245 E/art ( 7690): from void mono.android.view.View_OnClickListenerImplementor.n_onClick(android.view.View)

    Thanks for any help

  • jerchujerchu Member

    I had this same problem except with the native android API.

    In case you guys still haven't fixed it, I found that rebuilding the android project fixed this for me. It seems the Interface is only added when you rebuild the project.

    Hope this helps.

  • lesscodelesscode Member

    I'm having the exact same problem, falling at the first hurdle when it comes to my Xamarin app. No idea how to continue from here. Disappointing.

Sign In or Register to comment.