我在操作欄中有一個下拉式的AutoCompleteTextView。AutoCompleteTextView.getText()。toString()爲空
我試圖從AutoCompleteTextView中獲取文本並將其存儲到一個字符串中。
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.location2);//Location2 is my layout
android.support.v7.app.ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowCustomEnabled(true);
LayoutInflater inflater = (LayoutInflater) this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.location, null);//location is my custom action bar
actionBar.setCustomView(view);
Global global = new Global();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_dropdown_item, global.getUniversities());
AutoCompleteTextView textView = (AutoCompleteTextView) view
.findViewById(R.id.searchLocation);
textView.setAdapter(adapter);
...
}
public void changeLocation(View view){
View v = View.inflate(this, R.layout.location, null);
AutoCompleteTextView searchLocation = (AutoCompleteTextView)
v.findViewById(R.id.searchLocation);
String searchLocationText = searchLocation.getText().toString();
Toast.makeText(this, searchLocationText,
Toast.LENGTH_LONG).show();
...
}
<AutoCompleteTextView
android:id="@+id/searchLocation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:drawableLeft="@drawable/search"
android:paddingTop="15dp"
android:imeOptions="actionSearch"
android:inputType="textAutoComplete|textAutoCorrect"
android:textColor="#FFFFFF" >
<requestFocus />
</AutoCompleteTextView>
自動完成功能就像一個魅力......但是敬酒顯示字符串searchLocationText是空的...
什麼是不正確有關操作欄中的autocompletetextview?
如何將文本轉換爲字符串?
編輯: - 更多細節
我能夠在AutoCompleteTextView輸入,它與一個下拉列表自動填充和一切工作正常。但我試圖把這些信息轉換成字符串和字符串是空的...
錯誤:
java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.AutoCompleteTextView.getText()' on a null object reference
錯誤指行:
String searchLocationText = searchLocation.getText().toString();
你在'changeLocation充氣一個完全新的,不同的'AutoCompleteTextView'()上的活動視圖'方法,並試圖從中獲取文本那。我不確定'onCreate()'中有什麼'view',但這不是你在膨脹並在'changeLocation()'中找到'AutoCompleteTextView'的東西。 –
這讓人有一種感覺!讓我看看我是否可以相應地編輯我的程序,如果這是答案,那麼我會讓你知道 –
你忘記了太多的代碼。如何在'onCreate()'中獲得'view'? – tynn