java - 当我尝试添加监听器时,EditText 使应用程序崩溃

标签 java android

当我尝试将 TextChangedListener 添加到 EditText 时,即使我没有在 AfterTextChangedMethod 中编写任何内容,应用程序也会崩溃。我试图获取用户插入的字符串,但是一旦添加监听器,它就会崩溃。

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);
    }

最佳答案

试试这个,

    @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;
    }

关于java - 当我尝试添加监听器时,EditText 使应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42342474/

相关文章:

android - FTS3 在 ORMLite 中搜索?

iphone 开发限制

java - 在 javax.mail.MimeMessage 中设置发件人名称?

Java 子菜单 ActionListener 无法正常工作

java - 显示非英语垃圾字符的 jsp 页面

java - 从外部方法访问变量

java - 在 IntelliJ 中显示或打开我的插件

android - ShapeableImageView 的笔画被边框切割

android - iOS 相当于 Android BOOT_COMPLETED 和 ACTION_SHUTDOWN

Android - 打开 SQLite 数据库