Android AutoCompleteTextView在发行版中使应用程序崩溃

时间:2019-09-18 12:25:49

标签: java android autocomplete

我正在使用具有来自服务器动态数据的AutoCompleteTextView。调试版本在发布时会平稳运行。

java.lang.NullPointerException: throw with null exception
        at c10.getView(SourceFile:50)
        at android.widget.AbsListView.obtainView(AbsListView.java:2367)
        at android.widget.DropDownListView.obtainView(DropDownListView.java:305)
        at android.widget.ListView.measureHeightOfChildren(ListView.java:1326)
        at android.widget.ListPopupWindow.buildDropDown(ListPopupWindow.java:1233)
        at android.widget.ListPopupWindow.show(ListPopupWindow.java:596)
        at android.widget.AutoCompleteTextView.showDropDown(AutoCompleteTextView.java:1215)
        at android.widget.AutoCompleteTextView.updateDropDownForFilter(AutoCompleteTextView.java:1084)
        at android.widget.AutoCompleteTextView.onFilterComplete(AutoCompleteTextView.java:1066)
        at android.widget.Filter$ResultsHandler.handleMessage(Filter.java:285)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:165)
        at android.app.ActivityThread.main(ActivityThread.java:6375)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:912)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:802)

我保留用于自动完成适配器的模型,例如

-keep public www.example.MyModel {
  public *** get*();
  public void set*(***);
}

在build.gradle文件中将minifyEnabled和收缩资源设为false 使其正常工作,但同时启用这些功能会使应用程序崩溃,但异常除外。 我该怎么办才能用minifyEnabled和rinkeResources true解决此问题?

我正在使用以下适配器


public class MyAdapter extends ArrayAdapter<MyModel> {

    Context context;
    int resource, textViewResourceId;
    List<MyModel> items, tempItems, suggestions;

    public MyAdapter(Context context, int resource, int textViewResourceId, List<MyModel> items) {
        super(context, resource, textViewResourceId, items);
        this.context = context;
        this.resource = resource;
        this.textViewResourceId = textViewResourceId;
        this.items = items;
        tempItems = new ArrayList<MyModel>(items); // this makes the difference.
        suggestions = new ArrayList<MyModel>();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.row, parent, false);
        }
        MyModel mymodel = items.get(position);
        if (mymodel != null) {
            TextView lblName = (TextView) view.findViewById(R.id.tv_site_name);
            if (lblName != null)
                lblName.setText(mymodel.getName());
            TextView lblAddress = (TextView) view.findViewById(R.id.tv_site_title);
            if (lblAddress != null)
                lblAddress.setText(mymodel.getAddress().getAddress());
        }
        return view;
    }

    @Override
    public Filter getFilter() {
        return nameFilter;
    }
    @Override
    public MyModel getItem(int index) {
        return items.get(index);
    }


    /**
     * Custom Filter implementation for custom suggestions we provide.
     */
    Filter nameFilter = new Filter() {
        @Override
        public CharSequence convertResultToString(Object resultValue) {
            String str = ((MyModel) resultValue).getName();
            return str;
        }

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            if (constraint != null) {
                suggestions.clear();
                //filter method logic goes here
                for (MyModel people : tempItems) {
                    if (people.getName().toLowerCase().startsWith(constraint.toString().toLowerCase())) {
                        suggestions.add(people);
                    }
                }
                FilterResults filterResults = new FilterResults();
                filterResults.values = suggestions;
                filterResults.count = suggestions.size();
                return filterResults;
            } else {
                return new FilterResults();
            }
        }

        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            List<MyModel> filterList = (ArrayList<MyModel>) results.values;
            if (results != null && results.count > 0) {
                clear();
                for (MyModel people : filterList) {
                    add(people);
                    notifyDataSetChanged();
                }
            }
        }
    };
}

在调试版本中或将minifyEnabled和rinkeResources设置为false时,效果很好

0 个答案:

没有答案