【问题标题】:Why Data Can not Pass to Second Activity为什么数据不能传递给第二个活动
【发布时间】:2013-05-08 11:05:51
【问题描述】:

使用 XamarinStudio 及以下基于教程中示例的代码。这里有问题。

  1. 测试应用程序时是否需要从项目选项> Android 应用程序生成 AndroidManifest?

为什么即使我生成了 AndroidManifest 也没有数据通过,代码:

---活动一 [活动(标签=“HelloMultiScreen”,MainLauncher = true,图标=“@dr​​awable/icon”)] 公共类FirstActivity:活动 { 整数计数 = 1; protected override void OnCreate(捆绑包) { base.OnCreate(捆绑); //使用Main.axml中创建的UI SetContentView (Resource.Layout.Main); var showSecond = FindViewById (Resource.Id.showSecond); showSecond.Click += (sender, e) => { var second = new Intent(this, typeof(SecondActivity)); second.PutExtra("FirstData", "数据来自 FirstActivity"); StartActivity (typeof(SecondActivity)); }; } } ---活动二 [活动(标签=“第二活动”)] 公共类SecondActivity:活动 { protected override void OnCreate(捆绑包) { base.OnCreate(捆绑); // 在此处创建您的应用程序 SetContentView (Resource.Layout.Second); var label = FindViewById (Resource.Id.screen2Label); label.Text = Intent.GetStringExtra("FirstData") ?? “数据不可用”; } }

谢谢

【问题讨论】:

  • 您会在标签中看到“数据不可用”还是什么也没有?
  • 是的。我得到“数据不可用”

标签: xamarin.android


【解决方案1】:

好的,我自己重新制作项目时发现了问题。 问题在于这段代码:

var second = new Intent(this, typeof(SecondActivity));
second.PutExtra("FirstData", "Data from FirstActivity");
StartActivity (typeof(SecondActivity));

发生的情况是您使用正确的数据创建了一个 Intent。但是您在没有该数据的情况下开始了一项新活动。 要修复它,请将代码更改为:

var second = new Intent(this, typeof(SecondActivity));
second.PutExtra("FirstData", "Data from FirstActivity");
StartActivity(second);`

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-28
    • 2015-03-21
    • 2012-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多