2017-02-20 101 views
1

當我嘗試向我的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); 
    } 
+0

後logcat的輸出 –

+0

後的logcat爲@RahulGiradkar建議。另外,請張貼您的.xml代碼 - 我有一種感覺可能會出現一些ClassCastExceptions。 – rencsaridogan

+1

使用'getView()'而不是'getActivity()' – Piyush

回答

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; 
    } 
+0

它仍然會崩潰。因爲你還沒有改變_dsxtview_ – Piyush

+0

@dsmerald改變id爲文本視圖 – user2025187

+0

這可能會解決你的問題。您的代碼在創建之前會嘗試訪問主內容視圖的內容。膨脹行在那裏創建你的XML視圖。 – James

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

你是不是誇大你的觀點在你onCreateViewMethod第一。先膨脹你的視圖,然後找到子視圖。

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

希望它有幫助。

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方法

相關問題