Activity does not open

Asked

Viewed 91 times

0

Well, I created two Activities when I clicked on the button of the first one it should open the second one with information received from the first one. But that’s not what happens, where I’m wrong?

 using System;

using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
//Atividade Principal
namespace TabelaPeriodica
{
    [Activity (Label = "TabelaPeriodica", MainLauncher = true, Icon = "@drawable/icon")]
    public class MainActivity : Activity
    {


        protected override void OnCreate (Bundle bundle)
        {
            base.OnCreate (bundle);

            // Set our view from the "main" layout resource
            SetContentView (Resource.Layout.Main);

            // Get our button from the layout resource,
            // and attach an event to it
            EditText edt = FindViewById<EditText> (Resource.Id.edtsmb);
            Button bttn = FindViewById<Button> (Resource.Id.env);


            if (!string.IsNullOrEmpty(edt.Text))
            {
                bttn.Click += delegate {
                    var ress = new Intent (this, typeof(Result));
                    ress.PutExtra ("simbol", edt.Text);
                    StartActivity (ress);
                };


            }


        }

    }
}

Segunda Activity:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;

namespace TabelaPeriodica
{
    [Activity (Label = "Result")]           
    public class Result : Activity
    {

        protected override void OnCreate (Bundle bundle)
        {
            base.OnCreate (bundle);

            // Create your application here
            SetContentView (Resource.Layout.Result);

            TextView sm = FindViewById<TextView> (Resource.Id.tsimb);

            sm.Text = Intent.GetStringExtra ("simbol") ?? "Erro";

        }
    }
}
  • Log the application to help us. You have declared the second Activity in the application manifest?

1 answer

2


See that in the OnCreate of his MainActivity you are never adding button click Event Handler

if (!string.IsNullOrEmpty(edt.Text))
{
    bttn.Click += delegate {
        var ress = new Intent (this, typeof(Result));
        ress.PutExtra ("simbol", edt.Text);
        StartActivity (ress);
    };
}

The OnCreate, as its name already says is only called when Activity is created, and when it is created the if (!string.IsNullOrEmpty(edt.Text)) will always return false.

You need to change this check, if you should only show the second Activity when the user gives a value then the if should be inside this delegate, something like

bttn.Click += delegate {
    if (!string.IsNullOrEmpty(edt.Text))
    {
        var ress = new Intent (this, typeof(Result));
        ress.PutExtra ("simbol", edt.Text);
        StartActivity (ress);
    }
};

Browser other questions tagged

You are not signed in. Login or sign up in order to post.