使用onTextChanged在搜索字段中输入文本时Android应用程序崩溃

时间:2017-04-23 12:34:15

标签: java android listview nullpointerexception

我正在尝试将搜索功能添加到从Web服务调用列表的自定义适配器。但是,当我尝试在搜索文本字段中输入任何内容时,应用程序崩溃了。我正在接受

  

“java.lang.NullPointerException:尝试调用虚方法   'android.widget.Filter android.widget.ArrayAdapter.getFilter()'上   null对象引用“错误。

我明白这意味着什么,但我不知道用什么代替代码。

由于

public class CallSoapGetTicketHoldersForEvent extends AsyncTask<GetTicketHolderForEventParams, Void, String> {

    private Exception exception;

    @Override
    protected String doInBackground(GetTicketHolderForEventParams... params) {
        return params[0].foo.GetTicketHoldersForEvent(params[0].eventname,params[0].username,params[0].password);
    }

    protected void onPostExecute(String result) {

        // Calling Application class (see application tag in AndroidManifest.xml)
        Local.Set(getApplicationContext(), "TicketHolders", result);

         //sets list view for ticket holder information
        String[] RowData = result.toString().split(";");
        List<String> TicketholdersArray = new ArrayList<>();
        for(int x=0;x<RowData.length;x++)
        {
            TicketholdersArray.add(RowData[x].toString());
        }

        ListView listView1 = (ListView) findViewById(R.id.your_list_view_id);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(GuestlistActivity.this,R.layout.liststyle, TicketholdersArray);
        adapter.setDropDownViewResource(R.layout.spinnerstyledrop);
        listView1.setAdapter(adapter);

        edtSearch = (EditText) findViewById(R.id.edtsearch);

        lstSearch = (ListView)findViewById(R.id.LstSearch);

        lstSearch.setAdapter(adapter);

        edtSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int start, int count, int after) {}

            @Override
            public void onTextChanged(CharSequence charSequence, int start, int before, int count) {
                GuestlistActivity.this.adapter.getFilter().filter(charSequence);
            }

            @Override
            public void afterTextChanged(Editable s) {}
        });
    }
}

1 个答案:

答案 0 :(得分:0)

似乎您的过滤器集为空。确保您没有在ArrayAdapter中传递空值。