将AutocompleteTextview与Adapter和Filterable一起使用 - '空指针异常'

时间:2015-10-27 05:38:16

标签: android filter adapter autocompletetextview

我正在使用'AutocompleteTextView'和'GooglePlaces'来帮助用户输入位置信息。我正在关注“http://codetheory.in/google-place-api-autocomplete-service-in-android-application/”中的示例。它最初运作良好。然后我尝试按照教程中的建议将“Google徽标”添加到列表的末尾,我收到以下错误

java.lang.NullPointerException
            at com.simple.simpleplacecomplete.GooglePlacesAutoCompleteActivity$GooglePlacesAutocompleteAdapter.getView(GooglePlacesAutoCompleteActivity.java:176)
            at android.widget.AbsListView.obtainView(AbsListView.java:2743)
            at android.widget.ListPopupWindow$DropDownListView.obtainView(ListPopupWindow.java:1596)
            at android.widget.ListView.measureHeightOfChildren(ListView.java:1274)
            at android.widget.ListPopupWindow.buildDropDown(ListPopupWindow.java:1177)
            at android.widget.ListPopupWindow.show(ListPopupWindow.java:564)
            at android.widget.AutoCompleteTextView.showDropDown(AutoCompleteTextView.java:1119)
            at android.widget.AutoCompleteTextView.updateDropDownForFilter(AutoCompleteTextView.java:973)
            at android.widget.AutoCompleteTextView.onFilterComplete(AutoCompleteTextView.java:955)
            at android.widget.Filter$ResultsHandler.handleMessage(Filter.java:285)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:146)
            at android.app.ActivityThread.main(ActivityThread.java:5593)
            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:1283)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
            at dalvik.system.NativeStart.main(Native Method)

我尝试使用调试器,但我无法弄清楚问题。调试器也没有到达getView( )。请在下面找到我的代码。任何帮助都非常感谢。感谢。

class GooglePlacesAutocompleteAdapter extends ArrayAdapter implements Filterable {

    private ArrayList resultList;
    Context mContext;

    public GooglePlacesAutocompleteAdapter(Context context, int textViewResourceId) {
        super(context, textViewResourceId);
    }

    @Override
    public int getCount() {
        return resultList.size();
    }

    @Override
    public String getItem(int index) {
        return resultList.get(index).toString();
    }

     @Override
    public Filter getFilter() {

        Filter filter = new Filter() {
            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                FilterResults filterResults = new FilterResults();
                if (constraint != null) {
                    // Retrieve the autocomplete results.
                    resultList = autocomplete(constraint.toString());

                    // Footer
                    resultList.add("footer");

                    // Assign the data to the FilterResults
                    filterResults.values = resultList;
                    filterResults.count = resultList.size();
                    }
                return filterResults;
              }

            @Override
            protected void publishResults(CharSequence constraint, FilterResults results) {
                if (results != null && results.count > 0) {
                    notifyDataSetChanged();
                } else {
                    notifyDataSetInvalidated();
                   }
                }
            };
        return filter;
       }//end of getFilter


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        android.os.Debug.waitForDebugger();
        View view;


        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        int size =  resultList.size();

        if (position != (resultList.size() - 1))
            view = inflater.inflate(R.layout.list_item, null);
        else
            view = inflater.inflate(R.layout.image_item, null);
            view.setEnabled(false);
            view.setOnClickListener(null);


        if (position != (resultList.size() - 1)) {
            TextView autocompleteTextView = (TextView) view.findViewById(R.id.autoCompleteText);
            autocompleteTextView.setText(resultList.get(position).toString());
        }
        else {
            ImageView imageView = (ImageView) view.findViewById(R.id.logo_image);

        }

        return view;
    }// end of getView
 }//end of class GooglePlacesAutoCompleteAdapter

0 个答案:

没有答案