KWPro.net

Android AutoComplete Tutorial Amendment
By: conark
Published On: 1-10-2011

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.

AddThis Social Bookmark Button Sphere: Related Content

Trackbacks: (Trackback URL)

No Comments Posted Yet
July [August] September
Sun Mon Tue Wed Thu Fri Sat
31 1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31 1 2 3