[英]what to use instead of this if the target is not on main.axml
所以我想在我的項目中使用autoCompleteTextView,我的問題是autoCompleteTextView不在main.axml中 ,而是在pager_item.axml中,所以當我使用
ArrayAdapter autoCompleteAdapter = new ArrayAdapter(this, Android.Resource.Layout.SimpleDropDownItem1Line, autoCompleteOptions);
構建成功,但是程序開始崩潰,表示對象引用未設置為對象的實例。
當SetContentView設置為pager_item.axml時,它可以正常工作,但是我不想設置內容視圖,因為我仍然需要main.axml。
所以我的問題是,當我的autoCompleteTextView不在活動布局上時,我會用什么代替它 。
您可以使用LayoutInflater
膨脹的pager_item.axml
在MainActivity
。
Android.Views.View view;
view=LayoutInflater.Inflate(Resource.Layout.pager_item, this, false);
AutoCompleteTextView autotext=view.FindViewById(Resource.Id.at);
您能否舉例說明上下文參數是什么?
Android中有三種上下文 , 應用程序 , 活動和服務 。
讓我們看看Button的構造方法Button(上下文上下文) ,它需要一個Context參數。 如果使用new Button(this);
初始化它new Button(this);
在Activity中, this
是Activity,在Service中, this
是Service,在其他類中,您可以通過getApplication或getApplicationContext使用全局上下文。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.