Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
722 views
in Technique[技术] by (71.8m points)

c# - Xamarin FindViewById returns null

I am trying to make an android app using Xamarin so C#. I made two layouts and in each one of them I made tow buttons to navigate between them.I tried like this:

using System;

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


namespace Example
{
    [Activity(Label = "Example", MainLauncher = true, Icon = "@drawable/icon")]
    public class MainActivity : Activity
    {
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            this.SetContentView(Resource.Layout.Main);

            this.FindViewById<Button>(Resource.Id.ForwardButton).Click += this.Forward;
            this.FindViewById<Button>(Resource.Id.BackButton).Click += this.Back;
        }

        public void Forward(object sender, EventArgs e)
        {
            this.SetContentView(Resource.Layout.Main2);
        }

        public void Back(object sender, EventArgs e)
        {
            this.SetContentView(Resource.Layout.Main);
        }
    }
}

But every time when I start the app I get this errror: System.NullReferenceException has been thrown.Object reference not set to an instance of an object. Any advice or better idea?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You are setting Main as the layout for your activity and then in the following lines of code you are asking to find a button named Back at runtime which is not part of this layout. This means the following line will return null:

FindViewById<Button>(Resource.Id.BackButton)

Now if you do FindViewById<Button>(Resource.Id.BackButton).Click, you will definitely get a System.NullReferenceException.

EDIT:

In view of the comments, here is what you should do to achieve what you are looking for:

Create two different activities (Main1 and Main2). In Main1 you do:

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

        this.FindViewById<Button>(Resource.Id.ForwardButton).Click += this.Forward;
    }

    public void Forward(object sender, EventArgs e)
    {
        this.StartActivity (typeof(Main2));
    }

Then in Main2, you do:

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

        this.FindViewById<Button>(Resource.Id.BackButton).Click += this.Back;
    }

    public void Back(object sender, EventArgs e)
    {
        this.StartActivity (typeof(Main));
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...