Quang Lu
Quang Lu

Reputation: 1

Xamarin Android Error - Built Error

I am making a ListView with click button open List View. When I rebuild it, everything is good with no error. But when I built in the simulation section, it reports an error. Does anyone help me to fix that please. Here my code which reported error:

Error Unhandled Exception:

System.NullReferenceException: Object reference not set to an instance of an object.

public class Screen1 : Activity
{

    private List<string> mItems;

    private ListView mListView;


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

        Button clicknews = FindViewById<Button>(Resource.Id.btnlist);

        mListView = FindViewById<ListView>(Resource.Id.lstView);

        clicknews.Click += (object sender, EventArgs args) =>
        {               
            mItems = new List<string>();

            mItems.Add("Meal Plan");

            mItems.Add("Training Plan");

            ArrayAdapter<string> mAdapter = new ArrayAdapter<string>(this, 

           Android.Resource.Layout.SimpleListItem1, mItems);

           mListView.Adapter = mAdapter;

        };

        SetContentView(Resource.Layout.screen1);
    }
}

Upvotes: 0

Views: 27

Answers (1)

Jason
Jason

Reputation: 89082

you need to call SetContentView before you call FindViewByID

Upvotes: 1

Related Questions