Forum Xamarin Xamarin.Android

Cant request Location Updates from Location Manager. "Cast Not Valid"...

MrMeMrMe DEMember ✭✭
edited April 2017 in Xamarin.Android

Hi everybody,

im pretty hopeless... so im starting my app and i made a class that returns the users coordinates. I implemented the entire interface that the Location Manager needs, the only problem is, that i do not have an "onresume" in my class since its not an activity ( AND CANNOT BE ONE!)

So, in my main activity im doing this:

   protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        SetContentView(Resource.Layout.Main);
        btnMain = FindViewById<Button>(Resource.Id.startbutton);

        serviceToStart = new Intent(this, typeof(EggService)); // intent to start service class 

        RunThisShit(); 

    }

    void RunThisShit()
    {
        var StartGps = new GPs(this);

        btnMain.Click += (sender, e) => 
        {
            Log.Debug(TAG, "Button_Main has been clicked!");
            StartService(serviceToStart);

            StartGps.InitializeLocationManager();
            StartGps.StartLocationUpdates();

        };

And StartLocationUpates looks like this:

public void StartLocationUpdates()
{
    _locationManager.RequestLocationUpdates(_locationProvider, 0, 0, this);
}

but somehow, this doesnt work. It returns an unvalid cast.
As I said, usually this line of code would be in the on resume method and that did work! But now I need to have it in another class which is NOT an activity.
So why cant I call this method? Any help would be very much appreciated!!
THANKS :)

Best Answers

Answers

  • JohnMillerJohnMiller USForum Administrator, Xamarin Team Xamurai

    @MrMe,

    Can you share the exception message and the stack trace?

  • MrMeMrMe DEMember ✭✭
    edited April 2017

    @JohnMiller said:
    @MrMe,

    Can you share the exception message and the stack trace?

    Hi, thanks for trying to help me! :)

    This is the error message:
    Unhandled Exception:
    System.InvalidCastException: Specified cast is not valid.

    this is under callstack:

        [External Code] 
        0x63 in Android.Locations.LocationManager.RequestLocationUpdates at /Users/builder/data/lanes/3511/ce955cc0/source/monodroid/src/Mono.Android/platforms/android-24/src/generated/Android.Locations.LocationManager.cs:1113,5    C#
    

    0x15 in EggHatcherV6.GPs.StartLocationUpdates at C:\Users\MeGreatWinnerWithPen\Desktop\EggHatcherV6\EggHatcherV6\GPs.cs:105,13 C#

        0x3A in EggHatcherV6.Activity1. at C:\Users\MeGreatWinnerWithPen\Desktop\EggHatcherV6\EggHatcherV6\MainActivity.cs:54,17    C#
        0x14 in Android.Views.View.IOnClickListenerImplementor.OnClick at /Users/builder/data/lanes/3511/ce955cc0/source/monodroid/src/Mono.Android/platforms/android-24/src/generated/Android.Views.View.cs:1892,6 C#
        0x13 in Android.Views.View.IOnClickListenerInvoker.n_OnClick_Landroid_view_View_ at /Users/builder/data/lanes/3511/ce955cc0/source/monodroid/src/Mono.Android/platforms/android-24/src/generated/Android.Views.View.cs:1857,5   C#
    
  • JohnMillerJohnMiller USForum Administrator, Xamarin Team Xamurai

    @MrMe,

    What line of code is C:\Users\MeGreatWinnerWithPen\Desktop\EggHatcherV6\EggHatcherV6\GPs.cs:105,13 C#?

    Is that _locationManager.RequestLocationUpdates(_locationProvider, 0, 0, this);?

  • MrMeMrMe DEMember ✭✭

    @JohnMiller said:
    @MrMe,

    What line of code is C:\Users\MeGreatWinnerWithPen\Desktop\EggHatcherV6\EggHatcherV6\GPs.cs:105,13 C#?

    Is that _locationManager.RequestLocationUpdates(_locationProvider, 0, 0, this);?

    yes exactly. what do u read from this? and what is wrong? xD

  • MrMeMrMe DEMember ✭✭
    Accepted Answer

    so,ive been able to fix this by first implementing the java.lang.object part and also playing around a little bit with the inilializelocationmanager() method!

  • Terence.6876Terence.6876 USMember ✭✭

    Hi
    What I want to do is quite simple; provide a button that when the user clicks, connects to the Location service and retrieve the Longitude and Latitude of the current location. I've spent hours trying to achieve this using the sample code, but it does not work. I dont have any errors, but the OnLocationChanged() does not get fired at all. I am not trying to track location everytime it changes, all I want to do is pickup the current location when needed. Can anyone help please?

    Regards

    Terence

Sign In or Register to comment.