简体   繁体   中英

EditText crashes app when I try to add listener

When I try to add a TextChangedListener to my EditText the app crashes, even if I don't write anything inside the AfterTextChangedMethod. I'm trying to get the string inserted by the user, but as soon as I add my listener it crashes.

public class MainActivityFragment extends Fragment {

    ArrayList<HashMap<String,String>> city_list;

    public MainActivityFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        EditText search = (EditText) getActivity().findViewById(R.id.search_view);
        search.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {    }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {     }

            @Override
            public void afterTextChanged(Editable s) {
                String inserted = s.toString();
                for(int i=0; i< city_list.size();i++){
                    if(city_list.get(i).toString().equalsIgnoreCase(inserted)){
                        TextView id = (TextView) getActivity().findViewById(R.id.city_id);
                        id.setText(city_list.get(i).get("_id").toString());
                        TextView lat = (TextView) getActivity().findViewById(R.id.city_lat);
                        lat.setText(city_list.get(i).get("lat").toString());
                        TextView lon = (TextView) getActivity().findViewById(R.id.city_long);
                        lon.setText(city_list.get(i).get("lon").toString());
                    }
                }
            }
        });

        return inflater.inflate(R.layout.fragment_main, container, false);
    }

Try this,

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_main, container, false);

        EditText search = (EditText)view.findViewById(R.id.search_view);
        search.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String inserted = s.toString();
                for(int i=0; i< city_list.size();i++){
                    if(city_list.get(i).toString().equalsIgnoreCase(inserted)){
                        TextView id = (TextView) view .findViewById(R.id.city_id);
                        id.setText(city_list.get(i).get("_id").toString());
                        TextView lat = (TextView) view .findViewById(R.id.city_lat);
                        lat.setText(city_list.get(i).get("lat").toString());
                        TextView lon = (TextView) view .findViewById(R.id.city_long);
                        lon.setText(city_list.get(i).get("lon").toString());
                    }
                }
            }
        });


        return view;
    }
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_main, container, false);

    EditText search = (EditText)view.findViewById(R.id.search_view);
    search.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            String inserted = s.toString();
            for(int i=0; i< city_list.size();i++){
                if(city_list.get(i).toString().equalsIgnoreCase(inserted)){
                    TextView id = (TextView) view .findViewById(R.id.city_id);
                    id.setText(city_list.get(i).get("_id").toString());
                    TextView lat = (TextView) view .findViewById(R.id.city_lat);
                    lat.setText(city_list.get(i).get("lat").toString());
                    TextView lon = (TextView) getActivity()view .findViewById(R.id.city_long);
                    lon.setText(city_list.get(i).get("lon").toString());
                }
            }
        }
    });


    return view;
}
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
View view=    inflater.inflate(R.layout.fragment_main, container, false);
        EditText search = (EditText) view.findViewById(R.id.search_view);
  TextView id = (TextView) view.findViewById(R.id.city_id);
  TextView lat = (TextView) view.findViewById(R.id.city_lat);
  TextView lon = (TextView) view.findViewById(R.id.city_long);
        search.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String inserted = s.toString();
                for(int i=0; i< city_list.size();i++){
                    if(city_list.get(i).toString().equalsIgnoreCase(inserted)){

                      id.setText(city_list.get(i).get("_id").toString());

                       lat.setText(city_list.get(i).get("lat").toString());

                     lon.setText(city_list.get(i).get("lon").toString());
                    }
                }
            }
        });


        return view;
    }

You are not inflating your view in you onCreateViewMethod first. First Inflate your view and then find sub views.

Your app is crashing because you are trying to add a listener to a null reference object. Double check the ID you are assigning to your editext. Moreover try the below code.

Just return this in your onCreateView Method.

return inflater.inflate(R.layout.fragment_main, container, false);

Override onViewCreated method, use the incoming View variable to initialize your EditText.

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    EditText etSearch = (EditText) view.findViewById(R.id.search_view);

}

Then add textChangeListener to it.

   etSearch.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
           //your code here
        }
    });

Hope it helps.

Try this...

I think you want to use LayoutInflater for your view.

LayoutInflater inflater1 = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.fragment_main, container, false);
EditText search = (EditText)view.findViewById(R.id.search_view);

Then you can override TextChangedMethod method

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM