jettero
jettero

Reputation: 845

AutoCompleteTextView (ArrayAdapter) NullPointerException in PhoneGap

I'm adapting an old WebOS app to android and I'm using phonegap for nearly all of it (since the old code was javascript, this is convenient), but I wish to use a native AutoCompleteTextView for input. I'm getting a NullPointerException, probably due to my exotic hybrid app. But let me start at the beginning. I appended a footer (per a post I found here) like so

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    super.loadUrl("file:///android_asset/www/index.html");

    /* ... boring things here ... */

    // http://stackoverflow.com/questions/7320910/android-phonegap-with-native-controls
        View footer = View.inflate(getContext(), R.layout.footer, null);
        root.addView( footer );

    // setContentView(root);
    // http://www.kwpro.net/blogs/2011/1/10/1651_Android_AutoComplete_Tutorial_Amendment.html        
}   

I found some notes on a blog that suggested the solution to my problem might be to setContentView, but that doesn't seem to work. Anyway, I set the autocomplete text exactly once from javascript using this (via window.f.setAutoComplete() through a java interface).

public void setAutoComplete(String []s) {
    Log.d(TAG, "[native]: setting autocomplete text");

    AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.inpu);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_item, s);
    textView.setAdapter(adapter);
}

Anyway, everything seems to work. That is, the list of choices pops up and you can sometimes pick one (sometimes) before I get a NullPointerException that force closes the app. I feel ill equipped to deal with this one, so I thought I'd ask in public.

The exception dump looks like this:

W/dalvikvm( 1832): threadid=11: thread exiting with uncaught exception (group=0x409c01f8)
E/AndroidRuntime( 1832): FATAL EXCEPTION: WebViewCoreThread
E/AndroidRuntime( 1832): java.lang.NullPointerException
E/AndroidRuntime( 1832):        at android.widget.AbsListView$RecycleBin.getScrapView(AbsListView.java:5900)
E/AndroidRuntime( 1832):        at android.widget.AbsListView.obtainView(AbsListView.java:2003)
E/AndroidRuntime( 1832):        at android.widget.ListPopupWindow$DropDownListView.obtainView(ListPopupWindow.java:1168)
E/AndroidRuntime( 1832):        at android.widget.ListView.measureHeightOfChildren(ListView.java:1244)
E/AndroidRuntime( 1832):        at android.widget.ListPopupWindow.buildDropDown(ListPopupWindow.java:1095)
E/AndroidRuntime( 1832):        at android.widget.ListPopupWindow.show(ListPopupWindow.java:524)
E/AndroidRuntime( 1832):        at android.widget.AutoCompleteTextView.showDropDown(AutoCompleteTextView.java:1062)
E/AndroidRuntime( 1832):        at android.widget.AutoCompleteTextView.updateDropDownForFilter(AutoCompleteTextView.java:939)
E/AndroidRuntime( 1832):        at android.widget.AutoCompleteTextView.onFilterComplete(AutoCompleteTextView.java:921)
E/AndroidRuntime( 1832):        at android.widget.Filter$ResultsHandler.handleMessage(Filter.java:285)
E/AndroidRuntime( 1832):        at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 1832):        at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime( 1832):        at android.webkit.WebViewCore$WebCoreThread.run(WebViewCore.java:728)
E/AndroidRuntime( 1832):        at java.lang.Thread.run(Thread.java:856)

My best guess is that I'm running into that "only one thread can do UI stuff" ... ahh ... paradigm, but I'm not sure how to approach fixing that if that's the case, and if that's not the case, I have no idea what to try next.

Upvotes: 1

Views: 1000

Answers (1)

jettero
jettero

Reputation: 845

Never did get a response on this, but in case anyone else runs into it... The auto complete choices are part of the ui, so you have to ask the ui to update it. Pretty simple really. I had surmised it in the question itself, but didn't reason it out all the way.

public void setAutoComplete(String []s) {
    Log.d(TAG, "[native]: setting autocomplete text");

    autoCompleteArray = s;

    handler.post(new Runnable() {
        public void run() {
            hist.clear();
            for(int i=0; i<autoCompleteArray.length; i++)
                hist.add(autoCompleteArray[i]);
            hist.notifyDataSetChanged();
        }
    });
}

Upvotes: 1

Related Questions