Andrew Nguyen Andrew Nguyen - 15 days ago 13
Android Question

AutoCompleteTextView.getText().toString() is null

I have a drop down AutoCompleteTextView in an action bar.

I am attempting to get text from the AutoCompleteTextView and store it into a string.

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>


The autocomplete feature works like a charm... But the toast shows that String searchLocationText is empty...

What is incorrect about the autocompletetextview in the actionbar?

How can I get the text into a string?

Edit:- More details

I am able to type in the AutoCompleteTextView, it autocompletes with a drop down list and everything is working fine. But I try to turn that information into a string and the string is empty...

Error:

java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.AutoCompleteTextView.getText()' on a null object reference


Error refers to line:

String searchLocationText = searchLocation.getText().toString();

Answer

You need to get the text from the view you have attached to your layout. But you're inflating a new view here.

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();
    [...]
}

Instead find the view on the activity like in onCreate()

public void changeLocation(View view) {
    ActionBar actionBar = getSupportActionBar();
    view = actionBar.getCustomView();
    AutoCompleteTextView searchLocation = (AutoCompleteTextView) view
            .findViewById(R.id.searchLocation);
    String searchLocationText = searchLocation.getText().toString();
    Toast.makeText(this, searchLocationText,
            Toast.LENGTH_LONG).show();
    [...]
}