I found a good basic tutorial on doing a simple AutoComplete application using Android. However, the code was rather vague leading to a few problems (namely a NullPointerException). The trick to the tutorial is that your onCreate method should appear like this:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, items);
AutoCompleteTextView v = (AutoCompleteTextView)findViewById(R.id.Onara);
v.setThreshold(3);
v.setAdapter(adapter);
}
The key for me was making sure you have the setContentView method execute prior to running the findViewById method. It seems that setContentView creates the layout/UI objects such that you cannot access them from the main.xml file beforehand. This might not necessarily be the best explanation, but it's how I made sense of this.
Trackbacks: (Trackback URL)