Tinglovchini qo'shishga harakat qilganda, EditText ilovasi quladi

AfterTextChangedMethod ichida hech narsa yozish qilmagan bo'lsam ham, mening EditTextga TextChangedListenerni qo'shishga harakat qilsam, dastur cho'kadi. Men foydalanuvchi tomonidan kiritilgan mag'lubiyatni olishga urinmoqdaman, lekin tinglovchimni qo'shganimdan so'ng bu qulab tushadi.

public class MainActivityFragment extends Fragment {

    ArrayList> 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);
    }
1
getActivity() o'rniga getView() dan foydalaning
qo'shib qo'ydi muallif Piyush, manba
Logcat chiqqandan keyin
qo'shib qo'ydi muallif Rahul Giradkar, manba
Logcat-ni @RahulGiradkar deb taklif qildi. Bundan tashqari, iltimos, sizning .xml kodingizni joylashtiring - ba'zi ClassCastExceptions bo'lishi mumkin bo'lgan tuyg'u bor.
qo'shib qo'ydi muallif user4156995, manba

6 javoblar

    @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;
    }
0
qo'shib qo'ydi
@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;
}
0
qo'shib qo'ydi

Buni ko'ring,

    @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;
    }
0
qo'shib qo'ydi
Shunga qaramay, u qulab tushadi. TextView uchun identifikatorlarni o'zgartirmadingiz
qo'shib qo'ydi muallif Piyush, manba
Bu sizning muammoingizni echishda yordam beradi. Sizning kodingiz yaratilishingizdan oldin asosiy tarkib ko'rinishining tarkibiga kirishga harakat qiladi. U erda shamollash liniyasi xmldan ko'rinishni hosil qiladi.
qo'shib qo'ydi muallif James, manba
@Smerald matn ko'rinishi uchun idni o'zgartirdi
qo'shib qo'ydi muallif user2025187, manba
Bu uni hal qildi. Rahmat
qo'shib qo'ydi muallif Smerald Guci, manba

Sizning ilovangiz ishlamayapti, chunki siz null ma'lumot obyektiga tinglovchini qo'shishga urinmoqdasiz. Siz EDITTEST-ga tayinlangan identifikatorni ikki marta tekshiring. Bundan tashqari, quyidagi kodni sinab ko'ring.

Buni faqat onCreateView usulida qaytaring.

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

OnViewCreated usulini bekor qiling, EditText-ni ishga tushirish uchun kelgan View o'zgaruvchilaridan foydalaning.

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

}

Keyin unga textChangeListener qo'shing.

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

Umid qilamanki yordam beradi.

0
qo'shib qo'ydi

Avval sizda sizning ko'rinishni shaffof deb bo'lmaydi, oldindaCreateViewMethod. Avval fikringizni shamollash va pastki ko'rinishlarni topish.

0
qo'shib qo'ydi

Buni ko'ring ...

Sizning fikringiz uchun LayoutInflater'dan foydalanishni xohlayman deb o'ylayman.

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

Keyinchalik TextChangedMethod usulini bekor qilishingiz mumkin

0
qo'shib qo'ydi