EditText.addTextChangedListener(android.text.TextWatcher)' on a null object reference Textwatcher



我创建了一个自定义listView,并试图通过Edittext创建自定义过滤器/搜索过滤器。

edit_search或edit_text在AppBarmain(标准布局Android Studio(中。帮助我在代码中实现此信息,然后进行信息传输。

字典范围:

EditText editText;
ListView listView;
MyAdapter adapter;
MyAdapter myAdapter;
ArrayList<SingleRow> mylist;
TextWatcher textWatcher = new TextWatcher() {
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        myAdapter.getFilter().filter(s);
    }
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    }
    @Override
    public void afterTextChanged(Editable s) {
    }
};
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_dictionary, container, false);
    listView = view.findViewById(R.id.dictionaryList);
    editText = (EditText)view.findViewById(R.id.edit_search);
    editText.addTextChangedListener(textWatcher);
    mylist = new ArrayList<>();
    SingleRow singleRow;
    for (int i = 0;i<mFind.length;i++){
        singleRow = new SingleRow(mTitle[i],mDescriprion[i],images[i]);
        mylist.add(singleRow);
    }
    myAdapter = new MyAdapter(getActivity(),mylist);
    listView.setAdapter(myAdapter);
    return view;
}

MyAdapter类

public class MyAdapter extends BaseAdapter implements Filterable {
Context c;
ArrayList<SingleRow> originalArray,tempArray;
CustomFilter cs;
public MyAdapter(Context c, ArrayList<SingleRow> originalArray){
    this.c = c;
    this.originalArray = originalArray;
    this.tempArray = originalArray;
}
@Override
public Object getItem(int i) {
    return originalArray.get(i);
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
    LayoutInflater inflater = (LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View row = inflater.inflate(R.layout.row,null);
    ImageView images = row.findViewById(R.id.imageViewKanji);
    TextView myTitle = row.findViewById(R.id.textView1);
    TextView myDescription = row.findViewById(R.id.textView2);
    myTitle.setText(originalArray.get(i).getTitle());
    myDescription.setText(originalArray.get(i).getDescription());
    images.setImageResource(originalArray.get(i).getImage());

    return row;
}
@Override
public int getCount() {
    return originalArray.size();
}
@Override
public long getItemId(int i) {
    //return originalArray.indexOf(getItemId(i));
    return i;
}
@Override
public Filter getFilter() {
    if (cs == null) {
        cs = new CustomFilter();
    }
    return cs;
}
class CustomFilter extends Filter{
    @Override
    protected FilterResults performFiltering(CharSequence charSequence) {
        FilterResults results = new FilterResults();
        if (charSequence != null && charSequence.length() > 0) {
            charSequence = charSequence.toString().toUpperCase();
            ArrayList<SingleRow> filters = new ArrayList<>();
        for (int i = 0; i < tempArray.size(); i++) {
            if (tempArray.get(i).getTitle().toUpperCase().contains(charSequence)) {
                SingleRow singleRow = new SingleRow(tempArray.get(i).getTitle(), tempArray.get(i).getDescription(), tempArray.get(i).getImage());
                filters.add(singleRow);
            }
        }
        results.count = filters.size();
        results.values = filters;
    }
        else
        {
            results.count = tempArray.size();
            results.values = tempArray;
        }
        return results;
    }
    @Override
    protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
        originalArray = (ArrayList<SingleRow>)filterResults.values;
        notifyDataSetChanged();
    }
}

Singlerow class

public class SingleRow {
String title;
String description;
int image;
public SingleRow(String title, String description,int image){
    this.title = title;
    this.description = description;
    this.image = image;
}
public String getTitle() {
    return title;
}
public void setTitle(String title) {
    this.title = title;
}
public String getDescription() {
    return description;
}
public void setDescription(String description) {
    this.description = description;
}
public int getImage() {
    return image;
}
public void setImage(int image) {
    this.image = image;
}

}

2019-05-12 17:06:15.359 17262-17262/com.example.kanjilearn E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.kanjilearn, PID: 17262
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.kanjilearn/com.example.kanjilearn.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.EditText.addTextChangedListener(android.text.TextWatcher)' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2794)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2872)
        at android.app.ActivityThread.-wrap11(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1601)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6600)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:518)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:821)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.EditText.addTextChangedListener(android.text.TextWatcher)' on a null object reference
        at com.example.kanjilearn.DictionaryFragment.onCreateView(DictionaryFragment.java:419)

如果这还不够,那是我的github

我是Android开发的新手,因此请尽可能详细地解释并显示我应该采取的措施解决此问题。

您的字典范围的布局(r.layout.fragment_dictionary(中没有EDITTEXT。

相关内容

  • 没有找到相关文章

最新更新