当我尝试将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);
}
答案 0 :(得分:0)
试试这个,
@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;
}
答案 1 :(得分:0)
@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;
}
答案 2 :(得分:0)
@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;
}
答案 3 :(得分:0)
您不首先在onCreateViewMethod中夸大您的视图。首先给视图充气,然后查找子视图。
答案 4 :(得分:0)
您的应用程序崩溃了,因为您正在尝试将侦听器添加到空引用对象。仔细检查您分配给您的editext的ID。此外,请尝试以下代码。
只需在onCreateView方法中返回。
return inflater.inflate(R.layout.fragment_main, container, false);
覆盖onViewCreated方法,使用传入的View变量初始化EditText。
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
EditText etSearch = (EditText) view.findViewById(R.id.search_view);
}
然后将textChangeListener添加到它。
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
}
});
希望它有所帮助。
答案 5 :(得分:0)
试试这个......
我认为您希望使用LayoutInflater进行查看。
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);
然后你可以覆盖TextChangedMethod方法