方法之间的模糊呼叫

时间:2015-10-26 12:19:15

标签: c# android xamarin

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

  

C:\ Users \ nemesis \ Documents \ GitHub \ Murakami_kiev \ MurakamiKiev \ Menu Activities \ SoupesActivity.cs(5,5):错误CS0121:以下方法或属性之间的调用不明确:' 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"] + "г";
}

代码中有什么问题?

感谢帮助

3 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分: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 :(得分:0)

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

考虑这个方法

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);
  

无法从'object'转换为'short'

您尝试将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;
    }
}

在选择合适的方法时,编译器可以将此类视为Foo,将Object视为short,将bool视为Foo f = new Foo(); Bar(f); 。这就是你得到错误的原因

  

以下方法或属性之间的调用不明确:   'Bar(object)'和'Bar(short)'

{{1}}