0

I have a button, and two edittext fields. When I click the button I want the text value of edittext1 to be set as the text value of edittext2. How do I do this?

I want something like this:

button1.Click += (sender, e) =>{
string s = editText1.Text.ToString();
editText2.Text = s;
};

What I have right now:

public class MainActivity : Activity
{
    Button button1;
    EditText editText1;
    EditText editText2;

    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
        SetContentView(Resource.Layout.Main);

        button1 = FindViewById<Button>(Resource.Id.button1);
        EditText editText1 = FindViewById<EditText>(Resource.Id.EditText1);
        EditText editText2 = FindViewById<EditText>(Resource.Id.EditText2);

            button1.Click += (sender, e) =>
            {
                string value = editText1.Text.ToString();
//On the next line of code
//Unhandled Exception:
//System.NullReferenceException: Object reference not set to an instance of an object.
                editText2.Text = value;
            };
    }
}
Pille K
  • 1
  • 1
  • Your `editText2` is probably not inflated yet. Check out [this](https://stackoverflow.com/questions/3264610/findviewbyid-returns-null) question. – Tamás Szabó Jul 11 '17 at 08:17
  • @TamásSzabó Thank you! I had to set the SetContentView() before FindViewById() – Pille K Jul 11 '17 at 09:33

0 Answers0