3

我需要帮助才能使过滤工作。ListView 显示正确,但是当我在搜索字段中输入一个字母时,它会出错

java.lang.NullPointerException

和应用程序关闭。

这是我认为我必须修复但不知道如何修复的活动中的代码片段:

    protected void onPostExecute(String file_url) {
        // dismiss the dialog after getting all albums
        pDialog.dismiss();
        // updating UI from Background Thread
        runOnUiThread(new Runnable() {
            public void run() {
                /**
                 * Updating parsed JSON data into ListView
                 * */

                ListAdapter adapter = new SimpleAdapter(
                        WineSearchActivity_2.this, albumsList,
                        R.layout.activity_search_list_of_wines, new String[] {
                                TAG_wine_id, TAG_wine_name,
                                TAG_wine_country_id,
                                TAG_wine_country_flag_pic }, new int[] {
                                R.id.wine_id, R.id.wine_name,
                                R.id.wine_country_id, R.id.imageView2 });
                ListView lv = getListView();
                LayoutInflater inflater = getLayoutInflater();
                View header = inflater.inflate(R.layout.header,
                        (ViewGroup) findViewById(R.id.LinearLayoutHeader));
                View search_box = inflater.inflate(R.layout.search_box,
                        (ViewGroup) findViewById(R.id.RelativeLayoutSearchBox));
                lv.addHeaderView(header, null, false);
                lv.addHeaderView(search_box, null, false);

                // updating listview
                setListAdapter(adapter);

                inputSearch = (EditText) findViewById(R.id.inputSearch);

                inputSearch.addTextChangedListener(new TextWatcher() {

                    @Override
                    public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
                        // When user changed the Text
                        ((SimpleAdapter)WineSearchActivity_2.this.adapter).getFilter().filter(cs); 

                    }

                    @Override
                    public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                            int arg3) {
                        // TODO Auto-generated method stub


                    }

                    @Override
                    public void afterTextChanged(Editable arg0) {
                        // TODO Auto-generated method stub

                    }
                });
4

2 回答 2

6

全局设置您的 ListAdapter 适配器。我敢肯定,这会奏效。卡迈勒

于 2013-10-03T07:18:44.230 回答
1

Luksprog 提供的解决方案奏效了。

于 2013-07-19T11:26:37.773 回答