【问题标题】:This code is no working and error is shown at compilation?此代码不起作用,编译时显示错误?
【发布时间】:2013-11-08 09:34:58
【问题描述】:
using System;
using Android.App;
using Android.Os;
using Android.Widget;
using Dot42;
using Dot42.Manifest;
using Android.Location;


[assembly:UsesPermission(Android.Manifest.Permission.ACCESS_COARSE_LOCATION)]
[assembly:UsesPermission(Android.Manifest.Permission.INTERNET)]
[assembly:UsesPermission(Android.Manifest.Permission.ACCESS_FINE_LOCATION)]

[assembly: Application("simplegps")]

namespace simplegps
{
    [Activity]
    public class MainActivity : Activity
    {

        private LocationManager service;
        private bool enable;
        private string provider;

        protected override void OnCreate(Bundle savedInstance) 
        {
            base.OnCreate(savedInstance);
            SetContentView(R.Layouts.MainLayout);


            var txtprovider= FindViewById <TextView>(R.Ids.txtprovider);
            var gpsstatus= FindViewById <TextView>(R.Ids.gpsstatus);
            var txtcity = FindViewById<TextView>(R.Ids.txtcity);
            var txtlat = FindViewById<TextView>(R.Ids.txtlat);
            var txtlon = FindViewById<TextView>(R.Ids.txtlon);

            service=(LocationManager)GetSystemService(LOCATION_SERVICE);

            enable=service.IsProviderEnabled(LocationManager.GPS_PROVIDER);


            if(enable)
            {
                gpsstatus.Text="Gps enabled";
            }

            else
            {
                gpsstatus.Text="Gps not enabled";
                return;
            }

            var criteria = new Criteria{Accuracy = Criteria.ACCURACY_FINE};
            provider = service.GetBestProvider(criteria,false);
            var location = service.GetLastKnownLocation(provider);

            if(location !=null)
            {
                txtprovider.Text=provider;
                var latitude = location.Latitude;
                var longitude = location.Longitude;

                txtlat.Text=latitude.ToString();
                txtlon.Text=longitude.ToString();
            }

            else
            {
                txtprovider.Text="no location";
                return;
            }

            if(Geocoder.IsPresent())
            {
                Android.Location.Geocoder geo;
                Android.Location.Address adds;
                  adds=geo.GetFromLocation(location.GetLatitude(),location.GetLongitude(),1);

            }
        }
   }
}

错误信息: 它显示错误“无法将类型'Java.Util.IList'隐式转换为'Android.Location.Address'。存在显式转换(您是否缺少演员表?)(CS0266)”

【问题讨论】:

  • 您正在尝试将 List 类型转换为 Address 类型。虽然不知道在哪里。它可能在 dot42 代码中的某个位置。您应该将其报告为错误并尝试使用 Native JAVA 方式。

标签: geolocation street-address google-geocoder dot42


【解决方案1】:

这是失败的那一行:

adds = geo.GetFromLocation(location.GetLatitude(), location.GetLongitude(), 1)

geo.GetFromLocation 返回Java.Util.IList&lt;Address&gt;adds 的类型为 Address。因此编译错误。

使用索引运算符访问地址之一。

编辑

另外,你应该在使用之前初始化geo

Geocoder geo = new Geocoder(this, Locale.getDefault()); 

最后,GetFromLocation 可能返回 null 或空列表,因此请检查两者。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-03
    • 1970-01-01
    • 2021-08-21
    • 2020-10-10
    • 2022-01-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多