AutoCompleteTextView sqlite poplulation exception

那年仲夏 提交于 2019-12-02 11:34:12

I doubt believe OnItemSelected will ever be called by an AutoCompleteTextView, when the user clicks an item in the dropdown list the AutoComplete will only trigger an OnItemClick event.

Try moving your OnItemSelectedListener code into the OnItemClickListener.

Addition

SQLiteDatabase.query() will always return a valid Cursor, you need to check the size of the Cursor to see if it is valid:

    if(cursor != null){ // Not applicable

Try:

if(cursor.getCount() > 0)

Also you don't particularly need to convert your Cursor into a String array, a SimpleCursorAdapter will handle the details of this. But your approach will work as well (with a few changes).

Null Pointer Exception

If you are receiving a NPE here:

Cursor cursor = this.sqliteDBInstance.query(...);

Then simply sqliteDBInstance is null. Where do you initialize sqliteDBInstance? Do you have an open() method in SQLiteModelSearch, if so did you call it?

I've figured it out. the issue was that I had defined

public AutoCompleteTextView modelAutoComplete;
public AutoCompleteTextView makeAutoComplete;

Then then in function onCreate

I had defined:

AutoCompleteTextView modelAutoComplete = (AutoCompleteTextView) findViewById(R.id.autoCompleteModel);
AutoCompleteTextView makeAutoComplete = (AutoCompleteTextView) findViewById(R.id.autoCompleteModel);

I overlooked variable scope since both makeAutoComplete and modelAutoComplete where defined twice, one as a global class variable, and the second as a local function variable.

since the global variable was only defined and not initialized. The function initModelAutoComplete() was referencing the global public variable modelAutoComplete causing the NPE exception.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!