0

So I'm trying to use an autoCompleteTextView in my project and my problem is that the autoCompleteTextView is not in the main.axml, but in the pager_item.axml so when I use

ArrayAdapter autoCompleteAdapter = new ArrayAdapter(this, Android.Resource.Layout.SimpleDropDownItem1Line, autoCompleteOptions);

the build is succesful, but the progrem crash at the start saying Object reference not set to an instance of an object.

when the SetContentView is set to pager_item.axml it works fine, but i dont want to set the content view, because i still need the main.axml.

so my question is what sould i use instead of this when my autoCompleteTextView is not on the active layout.

1 Answers1

0

You can use LayoutInflater to inflate your pager_item.axml in your MainActivity.

Example

Android.Views.View view;
view=LayoutInflater.Inflate(Resource.Layout.pager_item, this, false);
AutoCompleteTextView autotext=view.FindViewById(Resource.Id.at);

Edit

could you give an example on what is the context parameter?

There are three kinds of Context in Android, Application, Activity and Service.

Let's see Button's construction method Button (Context context), it needs a Context parameter. If you initialize it using new Button(this);, in Activity, this is Activity, in Service, this is Service, in other class, you can use global Context by getApplication or getApplicationContext.

Robbit
  • 4,300
  • 1
  • 13
  • 29