Null GetStringExtra(Intent) - Xamarin.Droid

时间:2017-06-20 09:25:30

标签: c# android xamarin

我使用意图将数据从一个activity1传递给activity2。我在activity1中安慰了我的数据,我能够看到我发送给activity2的数据。但是,当我在activity2中接收数据时,我没有收到任何数据。我基本上遵循了使用意图的正确程序,但技巧不起作用。

问题:异步过程会影响意图的使用吗?

活性1

private async Task GetData()
{
    var firebase = new FirebaseClient(FirebaseUrl);
    var items = await firebase.Child("TableName").OnceAsync<Person>();

    foreach (var item in items)
    {
        Person details = new Person();
        details.Name = item.Object.Name;
        person.Add(details);
    }

    void OnItemClick(object sender, int position)
    {
        Intent peaceHeroDetail = new Intent(this, typeof(PersonDetailActivity));
        Intent.PutExtra(PersonDetailActivity.EXTRA_NAME, person[position].Name.ToString());
        // the value of what is being sent is seen
        Console.WriteLine("This is selected person " +person[position].Name);
    }
}

活性2

public const string EXTRA_NAME = "Person Name";
string PName = Intent.GetStringExtra(EXTRA_NAME);
//it is empty
Console.WriteLine("This is the received detail "+ PName);

1 个答案:

答案 0 :(得分:1)

由于您的评论显示这是一个错误,我删除了旧答案,并为您提供了此更新。

<强>更新

请查看以下文章:Pass data between activities

在您的第一项活动中,您应该更改此内容:

Intent peaceHeroDetail = new Intent(this, typeof(PersonDetailActivity));
Intent.PutExtra(PersonDetailActivity.EXTRA_NAME, person[position].Name.ToString());

到此:

Intent peaceHeroDetail = new Intent(this, typeof(PersonDetailActivity));
peaceHeroDetail.PutExtra(PersonDetailActivity.EXTRA_NAME, person[position].Name.ToString());

然后使用:StartActivity(peaceHeroDetail);