在图像仍在加载时过滤SimpleAdapter时应用程序崩溃

时间:2015-06-20 03:29:37

标签: java android listview

/** AsyncTask to parse json data and load ListView */
        private class ListViewLoaderTask extends AsyncTask<String, Void, String>{
            // Doing the parsing of xml data in a non-ui thread
            @Override
            protected String doInBackground(String... strJson) {
                try{
                    jObject = new JSONObject(strJson[0]);
                    StoreJSONParser countryJsonParser = new StoreJSONParser();
                    countryJsonParser.parse(jObject);
                }catch(Exception e){
                    Log.d("JSON Exception1",e.toString());
                }

                // Instantiating json parser class
                //StoreJSONParser countryJsonParser = new StoreJSONParser();

                try{
                    // Getting the parsed data as a List construct
                    countries = countryJsonParser.parse(jObject);
                }catch(Exception e){
                    Log.d("Exception",e.toString());
                }
                return null;
            }

        /** Invoked by the Android on "doInBackground" is executed */
        @Override
        protected void onPostExecute(String result) {

            // Setting adapter for the listview
            adapter = new SimpleAdapter(ChooseCategory.this, countries, R.layout.restaurant_list, from, to);
            mListView.setAdapter(adapter);
            Log.d("What is countries?", String.valueOf(countries));
            searchBox.addTextChangedListener(new TextWatcher() {

                public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
                    //adapter.getFilter().filter(cs);
                    adapter.getFilter().filter(cs.toString().trim());
                }

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

                }

                public void afterTextChanged(Editable arg0) {
                    // TODO Auto-generated method stub
                    //ChooseCategory.this.adapter.getFilter().filter(arg0);
                    //adapter.getFilter().filter();
                }
            });

            for(int i=0;i<adapter.getCount();i++){
                HashMap<String, Object> hm = (HashMap<String, Object>) adapter.getItem(i);
                String imgUrl = (String) hm.get("flag_path");
                ImageLoaderTask imageLoaderTask = new ImageLoaderTask();

                HashMap<String, Object> hmDownload = new HashMap<String, Object>();
                hm.put("flag_path",imgUrl);
                hm.put("position", i);

                // Starting ImageLoaderTask to download and populate image in the listview
                imageLoaderTask.execute(hm);
            }
        }
    } 

logcat错误

java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
            at android.widget.SimpleAdapter$SimpleFilter.performFiltering(SimpleAdapter.java:352)
            at android.widget.Filter$RequestHandler.handleMessage(Filter.java:234)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.os.HandlerThread.run(HandlerThread.java:61)

java.lang.NullPointerException
            at android.widget.SimpleAdapter.getCount(SimpleAdapter.java:93)
            at android.widget.AdapterView.checkFocus(AdapterView.java:713)
            at android.widget.AdapterView$AdapterDataSetObserver.onInvalidated(AdapterView.java:836)
            at android.widget.AbsListView$AdapterDataSetObserver.onInvalidated(AbsListView.java:6288)
            at android.database.DataSetObservable.notifyInvalidated(DataSetObservable.java:50)
            at android.widget.BaseAdapter.notifyDataSetInvalidated(BaseAdapter.java:59)
            at android.widget.SimpleAdapter$SimpleFilter.publishResults(SimpleAdapter.java:383)
            at android.widget.Filter$ResultsHandler.handleMessage(Filter.java:282)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)

只有当图像完全加载时,才能通过searchBox完美搜索。当我在图片仍在加载时在searchBox中输入内容时,它会崩溃。我试图谷歌但无法找到任何接近这个的解决方案。有人知道什么是错的吗?在此先感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

simple adapter中,它在mData上为空指针。构造适配器时,countries必须为null。