【问题标题】:Ambigious call between methods方法之间的模糊调用
【发布时间】:2015-10-26 21:03:50
【问题描述】:

我尝试在活动之间传递数据并出现错误:

C:\Users\nemesis\Documents\GitHub\Murakami_kiev\MurakamiKiev\Menu Activities\SoupesActivity.cs(5,5): 错误 CS0121: The call is ambiguous between the following methods or properties: 'Android.Content.Intent .PutExtra(string, short)' 和 'Android.Content.Intent.PutExtra(string, bool)' (CS0121) (MurakamiKiev)

活动 1 中的代码:

private void ParseAndDisplay1(JsonValue json) {
    TextView productname = FindViewById<TextView> (Resource.Id.posttittle);
    TextView price = FindViewById<TextView> (Resource.Id.price);
    TextView weight = FindViewById<TextView> (Resource.Id.weight);
    productname.Click += delegate {
        var intent404 = new Intent (this, typeof(SoupesDetailActivity1));

        JsonValue firstitem = json [81];

        intent404.PutExtra ("title", firstitem ["post_title"]);
        intent404.PutExtra ("price", firstitem ["price"] + " грн");
        intent404.PutExtra ("weight", firstitem ["weight"] + "г");
        StartActivity (intent404);
    };
}

接收属性时 Activity2 中的代码:

private void ParseAndDisplay(JsonValue json) {
    TextView productname = FindViewById<TextView>(Resource.Id.posttitle);
    TextView content = FindViewById<TextView>(Resource.Id.postcontent);
    TextView price = FindViewById<TextView>(Resource.Id.price);
    TextView weight = FindViewById<TextView>(Resource.Id.weight);
    //JsonValue firstitem = json[81];

    productname.Text = Intent.GetStringExtra("title");
    content.Text = firstitem["post_excerpt"];
    price.Text = firstitem["price"] + " грн";
    weight.Text = firstitem["weight"] + "г";
}

代码有什么问题?

谢谢你的帮助

【问题讨论】:

    标签: c# android xamarin


    【解决方案1】:

    JsonValue 继承自 object,当您调用 intent404.PutExtra 时,.NET 不知道他将调用哪个方法,要解决您的问题,您只需转换对象,如下所示:

    intent404.PutExtra("title", (string)firstitem["post_title"]);
    

    intent404.PutExtra("title", Convert.ToString(firstitem["post_title"]));
    

    MSDN 上的 JSON 值:

    https://msdn.microsoft.com/en-us/library/system.json.jsonvalue(v=vs.95).aspx

    我想它会解决你的问题。

    【讨论】:

    • 谢谢你帮了我这么多
    【解决方案2】:

    出现此错误是因为编译器没有指定足够的数据来理解要调用哪个方法。
    要解决您的任务,您必须在 PutExtra 方法调用中明确指定您的变量类型:

    intent404.PutExtra ("title", (firstitem ["post_title"]).ToString());
    intent404.PutExtra ("price", (firstitem ["price"] + " грн").ToString());
    intent404.PutExtra ("weight", (firstitem ["weight"] + "г").ToString());
    

    【讨论】:

      【解决方案3】:

      以前的答案显示了如何解决问题,但错误消息令人困惑,所以我将描述这是如何发生的。

      考虑这种方法

      private static void Bar(short value) { }
      private static void Bar(bool value) { }
      

      编译器应该决定运行什么方法,它是根据参数类型来决定的。

      编译器将选择第一种方法:

      short s = 0;
      Bar(s);
      

      编译器会选择第二种方法:

      bool b = true;
      Bar(b);
      

      如果编译器找不到适合你的参数类型的方法,它会给你一个错误:

      object obj = new object();
      Bar(obj);
      

      无法从“对象”转换为“短”

      您尝试将firstitem ["post_title"] 作为方法参数传递。这个参数的类型是JsonValue。你得到ambiguous call错误的原因是JsonValueimplicit cast operators

      考虑以下类:

      public class Foo
      {
          public static implicit operator short (Foo value)
          {
              return 0;
          }
      
          public static implicit operator bool (Foo value)
          {
              return false;
          }
      }
      

      编译器在选择合适的方法时,可以将此类视为FooObjectshortbool。这就是你得到错误的原因

      以下方法或属性之间的调用不明确: “条形(对象)”和“条形(短)”

      Foo f = new Foo();
      Bar(f);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-04-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多