【问题标题】:Cannot implicitly convert type 'Java.Lang.Object' to 'Android.Runtime.JavaDictionary<string, string>无法将类型“Java.Lang.Object”隐式转换为“Android.Runtime.JavaDictionary<string, string>
【发布时间】:2018-01-09 03:34:09
【问题描述】:

我正在我的应用上应用 Google Places Api 并将 Java 转换为 C# 代码。但是当我在列表中获取附近的地方时,我收到了这个错误:

无法将类型“Java.Lang.Object”隐式转换为“Android.Runtime.JavaDictionary”。存在显式转换(您是否缺少演员表?)

private void showNearbyPlaces(JavaList<JavaDictionary<string, string>> placeList)
    {
        for (int eachPlace = 0; eachPlace < placeList.Size(); eachPlace++)
        {

            MarkerOptions markerOptions = new MarkerOptions();
            JavaDictionary<string, string> googlePlace = placeList.Get(eachPlace); /*This is the error*/
        }
    }

代码截图

有人知道如何解决这个问题吗?谢谢你。

【问题讨论】:

    标签: c# android xamarin.android


    【解决方案1】:

    您可以使用System.Collections.Generic.List 代替JavaList,例如:

    using System.Linq;
    
    ...
    
    private void showNearbyPlaces(List<JavaDictionary<string, string>> placeList)
    {
        for (int eachPlace = 0; eachPlace < placeList.Count; eachPlace++)
        {
            JavaDictionary<string, string> googlePlace = placeList.ElementAt(eachPlace);
        }
    }
    

    【讨论】:

      【解决方案2】:

      尝试显式转换:

      JavaDictionary<string, string> googlePlace = (type)placeList.Get(eachPlace)
      

      如果这不起作用,我建议将 placeList 转换为更原始的对象(最有可能是字符串),然后将其重构为字典:

      JavaDictionary<string, string> googlePlace = placeList.Get(eachPlace).ConvertToString();
      
      public JavaDictionary<string, string> ConvertToString(this [placeListType] placeList)
      {
          // Create a dictionary
      
          // foreach object in placeList
          // Get the first string and assign it
          // Get the second string and assign it
          // Create a new object with them as parameters
          // Add object to dictionary
      
          // Return dictionary
      } 
      

      【讨论】:

        【解决方案3】:

        像这样使用 C# IDictionary,

        IDictionary<string, string> placeList= new Dictionary<string, string>();
        placeList.Add(new KeyValuePair<string, string>("your string", "your new string"));
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-01-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-11-11
          • 2013-02-22
          相关资源
          最近更新 更多