當我嘗試向我的EditText添加TextChangedListener時,應用程序崩潰,即使我沒有在AfterTextChangedMethod中寫入任何內容。我試圖讓用戶插入的字符串,但只要我添加我的偵聽器它崩潰。當我嘗試添加偵聽器時,EditText崩潰了應用程序
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);
}
後logcat的輸出 –
後的logcat爲@RahulGiradkar建議。另外,請張貼您的.xml代碼 - 我有一種感覺可能會出現一些ClassCastExceptions。 – rencsaridogan
使用'getView()'而不是'getActivity()' – Piyush