Xamarin:无法注册位置更新。 “指定的演员无效” [英] Xamarin: Cant register for location updates. "Specified cast not valid"

查看:204
本文介绍了Xamarin:无法注册位置更新。 “指定的演员无效”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在开发一个应用程序,它只会将您的坐标返回给用户。只要我在我的主要活动中使用它,这工作得很好。
现在我把所有这些都变成了它自己的类。现在,当我开始这个类时,发生的第一件事是这行代码:

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

这应该让我的手机开始侦听位置更新。但是,当我打电话给这条线时,我得到:



System.InvalidCastException:指定的转换无效。

这是为什么发生?
另外,我还需要这些代码吗?因为InitializeLocationManager()方法应该为我做这件事,对吗?


感谢:)

解决方案

我今天遇到了同样的错误,并找到了解决方案:确保您传递给您的服务

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

作为从Java.Lang.Object继承的最后一个参数,如:

  public class GpsService:Java.Lang.Object,ILocationListener 
{
...
}


so i was working on an app, that would simply give your coordinates back to the user. This worked fine as long as I had it in my main activity. Now i changed all of this into its own class. Now, when I start this class the first thing that happens is this line of code:

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

This SHOULD now have my phone start to listen for location updates. But when i call this line, I get:

System.InvalidCastException: Specified cast is not valid.

Why is this happening? Also, Do I even need this code? Because the method "InitializeLocationManager()" should do this for me, right?

Thanks :)

解决方案

I ahd the same error today and found the solution: make sure the service you're passing to

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

as the last arguments inherits from Java.Lang.Object, like:

public class GpsService : Java.Lang.Object, ILocationListener
{
    ...
}

这篇关于Xamarin:无法注册位置更新。 “指定的演员无效”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆