为什么数据无法传递给第二个活动

时间:2013-05-14 08:38:58

标签: xamarin.android

使用XamarinStudio及以下代码基于本教程中的Sample。这里是问题。

  1. 我是否需要从Project Option>生成AndroidManifest?测试应用程序时的Android应用程序?
  2. 为什么没有数据传递,即使我已经生成了AndroidManifest,代码:

    
    ---Activity 1  
    
    [Activity (Label = "HelloMultiScreen", MainLauncher = true,Icon = "@drawable/icon")]
    
        public class FirstActivity : Activity
    
        {
    
            int count = 1;
    
    
    
            protected override void OnCreate (Bundle bundle)
    
            {
    
                base.OnCreate (bundle);
    
    
    
                //Use UI created in Main.axml
    
                SetContentView (Resource.Layout.Main);
    
    
    
                var showSecond = FindViewById (Resource.Id.showSecond);
    
                showSecond.Click += (sender, e) => {
    
                    var second = new Intent(this, typeof(SecondActivity));
    
                    second.PutExtra("FirstData", "Data from FirstActivity");
    
                    StartActivity (typeof(SecondActivity));
    
                };
    
            }
    
        }
    
    
    ---Activity 2
    
        [Activity (Label = "SecondActivity")]           
    
        public class SecondActivity : Activity
    
        {
    
            protected override void OnCreate (Bundle bundle)
    
            {
    
                base.OnCreate (bundle);
    
    
                // Create your application here
    
    
                SetContentView (Resource.Layout.Second);
    
                var label = FindViewById (Resource.Id.screen2Label);
    
                label.Text = Intent.GetStringExtra("FirstData") ?? "Data not available";
    
            }
    
        }
    
    

    由于

1 个答案:

答案 0 :(得分:0)

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

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