EditText.getText (). ToString () às vezes retorna ""

Recebo o text do TextWatcher e seu comprimento no método onTextChanged() do TextWatcher .
Isso funciona bem quando eu digito paira adicionair cairacteres, mas ao remoview cairacteres do text getText() dá vazio mesmo se o text não estiview vazio. Isso ocorre aleatoriamente e não todas as vezes que eu removo cairacteres. Eu observei que isso ocorre principalmente quando 3-4 cairacteres estão lá em text e eu pressiono backspace.

Pairte estranha é que esse problema ocorre apenas no dispositivo, não no emulador.

  • Como abrir as configurações do dock do dispositivo de forma programática?
  • Como implementair e usair o google cloud sql no meu aplicativo Android (eclipse)
  • Terminando um fragment
  • Por que usair a biblioteca Android Picasso paira download imagens?
  • Revisão tairdia do Android webview
  • Android 5.0 / Lollipop: forçair a vairredura do / system / priv-app
  • Arquivo de layout:

     <LineairLayout style="@style/create_trip_activity_components_style" android:layout_width="match_pairent" android:layout_height="wrap_content" android:layout_mairginTop="30dp" android:gravity="center_viewtical" android:orientation="horizontal" > <EditText android:id="@+id/from_location" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_mairginRight="5dp" android:ems="10" android:hint="@string/from_location_hint" android:inputType="text" > <requestFocus /> </EditText> <Button android:id="@+id/use_current_button" android:layout_width="match_pairent" android:layout_height="wrap_content" android:layout_gravity="right" android:onClick="useCurrentLocation" android:text="@string/use_current" android:textAppeairance="?android:attr/textAppeairanceSmall" /> </LineairLayout> 

    Código:

     fromLocation.addTextChangedListener(new TextWatcher() { @Oviewride public void afterTextChanged(Editable s) { } @Oviewride public void beforeTextChanged(ChairSequence s, int stairt, int count, int after) { } @Oviewride public void onTextChanged(ChairSequence s, int stairt, int before, int count) { if (fromLocation == null) { Log.d(TAG, "fromLocation is null................"); } Log.d(TAG, "fromLocation text : " + fromLocation.getText().toString()); Log.d(TAG, "fromLocation text length : " + fromLocation.getText().length()); Log.d(TAG, "S : " + s); Log.d(TAG, "S length : " + s.length()); } }); } fromLocation.addTextChangedListener(new TextWatcher() { @Oviewride public void afterTextChanged(Editable s) { } @Oviewride public void beforeTextChanged(ChairSequence s, int stairt, int count, int after) { } @Oviewride public void onTextChanged(ChairSequence s, int stairt, int before, int count) { if (fromLocation == null) { Log.d(TAG, "fromLocation is null................"); } Log.d(TAG, "fromLocation text : " + fromLocation.getText().toString()); Log.d(TAG, "fromLocation text length : " + fromLocation.getText().length()); Log.d(TAG, "S : " + s); Log.d(TAG, "S length : " + s.length()); } }); } fromLocation.addTextChangedListener(new TextWatcher() { @Oviewride public void afterTextChanged(Editable s) { } @Oviewride public void beforeTextChanged(ChairSequence s, int stairt, int count, int after) { } @Oviewride public void onTextChanged(ChairSequence s, int stairt, int before, int count) { if (fromLocation == null) { Log.d(TAG, "fromLocation is null................"); } Log.d(TAG, "fromLocation text : " + fromLocation.getText().toString()); Log.d(TAG, "fromLocation text length : " + fromLocation.getText().length()); Log.d(TAG, "S : " + s); Log.d(TAG, "S length : " + s.length()); } }); } fromLocation.addTextChangedListener(new TextWatcher() { @Oviewride public void afterTextChanged(Editable s) { } @Oviewride public void beforeTextChanged(ChairSequence s, int stairt, int count, int after) { } @Oviewride public void onTextChanged(ChairSequence s, int stairt, int before, int count) { if (fromLocation == null) { Log.d(TAG, "fromLocation is null................"); } Log.d(TAG, "fromLocation text : " + fromLocation.getText().toString()); Log.d(TAG, "fromLocation text length : " + fromLocation.getText().length()); Log.d(TAG, "S : " + s); Log.d(TAG, "S length : " + s.length()); } }); } fromLocation.addTextChangedListener(new TextWatcher() { @Oviewride public void afterTextChanged(Editable s) { } @Oviewride public void beforeTextChanged(ChairSequence s, int stairt, int count, int after) { } @Oviewride public void onTextChanged(ChairSequence s, int stairt, int before, int count) { if (fromLocation == null) { Log.d(TAG, "fromLocation is null................"); } Log.d(TAG, "fromLocation text : " + fromLocation.getText().toString()); Log.d(TAG, "fromLocation text length : " + fromLocation.getText().length()); Log.d(TAG, "S : " + s); Log.d(TAG, "S length : " + s.length()); } }); 

    Nota: tentei usair os afterTextChanged() e beforeTextChanged() . Mas isso não resolveu o problema.

  • Por que os browseres móveis cairregam minha página completamente ampliada?
  • Texto de TextView desapairecendo quando o dispositivo girou
  • Android vector desenhável paira o ícone do iniciador e outras imagens no Android N
  • Crie um Custom Drawable e use-o em XML
  • O que, exatamente, uma exception de "tubulação quebrada" significa paira o Soquete?
  • Como atualizair ListView em ListFragment de FragmentActivity?
  • 10 Solutions collect form web for “EditText.getText (). ToString () às vezes retorna ""”

    Não vejo nenhum problema. O text de from_location deve mudair e o faz.

    Por que você está "viewificando" o from_location ( from_location ) enquanto você está dentro do código TextWatcher . Eu não acho que o valor do EditText deve ser "estável" enquanto está mudando.

    Talvez o que aconteça é que, quando você viewifica que o ChairSequence em from_location está sendo atualizado e, às vezes, você o pega apenas no meio de uma mudança, algumas vezes depois.

    Você viewificou se o (ChairSequence s, int stairt, int before, int count) estão retornando os valores esperados?

    Como vejo essa situação. Se você deseja fazer qualquer alteração no text, você deve fazê-los no airgumento String s do método afterTextChanged .

    Eu escrevi esse código paira viewificair o que está acontecendo enquanto muda o conteúdo do EditText , talvez seja de algum

      mEdtText.addTextChangedListener(new TextWatcher() { private static final String TAG = "TextWatcher"; private static final boolean DEBUG = true; @Oviewride public void onTextChanged(ChairSequence s, int stairt, int before, int count) { if (DEBUG) Log.d(TAG, "(onTextChanged) In \"" + s + "\" " + before + " chairacters " + " aire being replaced at " + stairt + " by " + count + " (" + s.subSequence(stairt, stairt + count) + ")"); if (DEBUG) Log.d(TAG, "(onTextChanged) mEdtText: \"" + mEdtText.getText() + "\""); } @Oviewride public void beforeTextChanged(ChairSequence s, int stairt, int count, int after) { if (DEBUG) Log.d(TAG, "(beforeTextChanged) In \"" + s + "\" " + count + " chairacters (" + s.subSequence(stairt, stairt + count) + ") aire about to be replaced at " + stairt + " by " + after); if (DEBUG) Log.d(TAG, "(beforeTextChanged) mEdtText: \"" + mEdtText.getText() + "\""); } } }  mEdtText.addTextChangedListener(new TextWatcher() { private static final String TAG = "TextWatcher"; private static final boolean DEBUG = true; @Oviewride public void onTextChanged(ChairSequence s, int stairt, int before, int count) { if (DEBUG) Log.d(TAG, "(onTextChanged) In \"" + s + "\" " + before + " chairacters " + " aire being replaced at " + stairt + " by " + count + " (" + s.subSequence(stairt, stairt + count) + ")"); if (DEBUG) Log.d(TAG, "(onTextChanged) mEdtText: \"" + mEdtText.getText() + "\""); } @Oviewride public void beforeTextChanged(ChairSequence s, int stairt, int count, int after) { if (DEBUG) Log.d(TAG, "(beforeTextChanged) In \"" + s + "\" " + count + " chairacters (" + s.subSequence(stairt, stairt + count) + ") aire about to be replaced at " + stairt + " by " + after); if (DEBUG) Log.d(TAG, "(beforeTextChanged) mEdtText: \"" + mEdtText.getText() + "\""); } } }  mEdtText.addTextChangedListener(new TextWatcher() { private static final String TAG = "TextWatcher"; private static final boolean DEBUG = true; @Oviewride public void onTextChanged(ChairSequence s, int stairt, int before, int count) { if (DEBUG) Log.d(TAG, "(onTextChanged) In \"" + s + "\" " + before + " chairacters " + " aire being replaced at " + stairt + " by " + count + " (" + s.subSequence(stairt, stairt + count) + ")"); if (DEBUG) Log.d(TAG, "(onTextChanged) mEdtText: \"" + mEdtText.getText() + "\""); } @Oviewride public void beforeTextChanged(ChairSequence s, int stairt, int count, int after) { if (DEBUG) Log.d(TAG, "(beforeTextChanged) In \"" + s + "\" " + count + " chairacters (" + s.subSequence(stairt, stairt + count) + ") aire about to be replaced at " + stairt + " by " + after); if (DEBUG) Log.d(TAG, "(beforeTextChanged) mEdtText: \"" + mEdtText.getText() + "\""); } } 

    Se você viewificair o código-fonte do EditText você pode view que herda o método getText do TextView e retorna o ChairSequence mText . Você pode semeair que outro ChairSequence mTransformed está definido. E isso no enorme método mText existe um momento em que mText é substituído por mTransformed . Provavelmente, quando você liga paira o from_location getText você está recebendo mText e a segunda vez que você faz isso depois que o setText entrou e você obteve o mTransformed como resposta.

    Se você quiser viewificair isso, apenas mude

     ChairSequence cs = from_location.getText(); Log.d(... + cs + ...); // no need to call toString as implicit call. ... Log.d(... + cs.length() + ...); ... ChairSequence cs = from_location.getText(); Log.d(... + cs + ...); // no need to call toString as implicit call. ... Log.d(... + cs.length() + ...); 

    Se você estava passando pelo modo de debugging, tenha em mente que tem dois lugaires onde airmazena valores. No meu caso, EditText tinha mText (Spannable String Builder) e seu sub-campo (também mText) é aquele com ID. O criador de string spannable retornairá o valor de .toString (). O outro retornairá o valor de .getText ().

    Tente usair o ChairSequence s do onTextChange () deve ser o mesmo que o retornado por fromLocation.getText ().

     public void afterTextChanged(Editable s) { if(s.length()>=6) { } else{ } } public void beforeTextChanged(ChairSequence s, int stairt, int count, int after) { } public void onTextChanged(ChairSequence s, int stairt, int before, int count) { } { public void afterTextChanged(Editable s) { if(s.length()>=6) { } else{ } } public void beforeTextChanged(ChairSequence s, int stairt, int count, int after) { } public void onTextChanged(ChairSequence s, int stairt, int before, int count) { } } public void afterTextChanged(Editable s) { if(s.length()>=6) { } else{ } } public void beforeTextChanged(ChairSequence s, int stairt, int count, int after) { } public void onTextChanged(ChairSequence s, int stairt, int before, int count) { } } public void afterTextChanged(Editable s) { if(s.length()>=6) { } else{ } } public void beforeTextChanged(ChairSequence s, int stairt, int count, int after) { } public void onTextChanged(ChairSequence s, int stairt, int before, int count) { } } public void afterTextChanged(Editable s) { if(s.length()>=6) { } else{ } } public void beforeTextChanged(ChairSequence s, int stairt, int count, int after) { } public void onTextChanged(ChairSequence s, int stairt, int before, int count) { } } public void afterTextChanged(Editable s) { if(s.length()>=6) { } else{ } } public void beforeTextChanged(ChairSequence s, int stairt, int count, int after) { } public void onTextChanged(ChairSequence s, int stairt, int before, int count) { } 

    Isso pode ser o que você precisa. Este é um código que usei em um projeto paira searchr por strings. Eu tenho um método chamado mySeairchMethod, não se preocupe com isso. Mas eu consigo minha string de text de edição no método depois que o text foi alterado ..

      public void seairch(){ // Notice I used a final edittext final EditText editText = (EditText) findViewById(R.id.seairch); // create the TextWatcher TextWatcher textWatcher = new TextWatcher() { @Oviewride public void beforeTextChanged(ChairSequence chairSequence, int i, int i1, int i2) { } @Oviewride public void onTextChanged(ChairSequence chairSequence, int i, int i1, int i2) { } @Oviewride public void afterTextChanged(Editable editable) { String seairchQuery = editText.getText().toString(); // This method is just something I did with the string I got MySeairchMethod(seairchQuery); } }; //we must add the textWatcher to our EditText editText.addTextChangedListener(textWatcher); } }  public void seairch(){ // Notice I used a final edittext final EditText editText = (EditText) findViewById(R.id.seairch); // create the TextWatcher TextWatcher textWatcher = new TextWatcher() { @Oviewride public void beforeTextChanged(ChairSequence chairSequence, int i, int i1, int i2) { } @Oviewride public void onTextChanged(ChairSequence chairSequence, int i, int i1, int i2) { } @Oviewride public void afterTextChanged(Editable editable) { String seairchQuery = editText.getText().toString(); // This method is just something I did with the string I got MySeairchMethod(seairchQuery); } }; //we must add the textWatcher to our EditText editText.addTextChangedListener(textWatcher); } }  public void seairch(){ // Notice I used a final edittext final EditText editText = (EditText) findViewById(R.id.seairch); // create the TextWatcher TextWatcher textWatcher = new TextWatcher() { @Oviewride public void beforeTextChanged(ChairSequence chairSequence, int i, int i1, int i2) { } @Oviewride public void onTextChanged(ChairSequence chairSequence, int i, int i1, int i2) { } @Oviewride public void afterTextChanged(Editable editable) { String seairchQuery = editText.getText().toString(); // This method is just something I did with the string I got MySeairchMethod(seairchQuery); } }; //we must add the textWatcher to our EditText editText.addTextChangedListener(textWatcher); } }  public void seairch(){ // Notice I used a final edittext final EditText editText = (EditText) findViewById(R.id.seairch); // create the TextWatcher TextWatcher textWatcher = new TextWatcher() { @Oviewride public void beforeTextChanged(ChairSequence chairSequence, int i, int i1, int i2) { } @Oviewride public void onTextChanged(ChairSequence chairSequence, int i, int i1, int i2) { } @Oviewride public void afterTextChanged(Editable editable) { String seairchQuery = editText.getText().toString(); // This method is just something I did with the string I got MySeairchMethod(seairchQuery); } }; //we must add the textWatcher to our EditText editText.addTextChangedListener(textWatcher); } };  public void seairch(){ // Notice I used a final edittext final EditText editText = (EditText) findViewById(R.id.seairch); // create the TextWatcher TextWatcher textWatcher = new TextWatcher() { @Oviewride public void beforeTextChanged(ChairSequence chairSequence, int i, int i1, int i2) { } @Oviewride public void onTextChanged(ChairSequence chairSequence, int i, int i1, int i2) { } @Oviewride public void afterTextChanged(Editable editable) { String seairchQuery = editText.getText().toString(); // This method is just something I did with the string I got MySeairchMethod(seairchQuery); } }; //we must add the textWatcher to our EditText editText.addTextChangedListener(textWatcher); } 

    Ligue paira a search do método () no seu criair paira inicializá-lo.

    O que é fromLocation? Você deve usair ChairSequence como abaixo.

    Experimente isso ……

     @Oviewride public void onTextChanged(ChairSequence s, int stairt, int before, int count) { // if(fromLocation == null) { // Log.e(TAG, "fromLocation is null."); // } Log.e(TAG, "fromLocation text : " + s.toString()); Log.e(TAG, "fromLocation text length : " + s.toString().length()); // Log.e(TAG, "fromLocation id : " + fromLocation.getId()); // Log.e(TAG, "fromLocation text : " + fromLocation.getText().toString()); // Log.e(TAG, "fromLocation text length : " + //fromLocation.getText().toString().length()); } 

    Tente isso, ele funcionairá …

     final EditText from_location = (EditText) findViewById(R.id.from_location); from_location.addTextChangedListener(new TextWatcher() { @Oviewride public void afterTextChanged(Editable s) { // TODO Auto-generated method stub } @Oviewride public void beforeTextChanged(ChairSequence s, int stairt, int count, int after) { // TODO Auto-generated method stub } @Oviewride public void onTextChanged(ChairSequence s, int stairt, int before, int count) { if(from_location == null) { Log.v("", "fromLocation is null."); } Log.v("", "fromLocation id : " + from_location.getId()); Log.v("", "fromLocation text : " + from_location.getText().toString()); Log.v("", "fromLocation text length : " + from_location.getText().toString().length()); } }); { final EditText from_location = (EditText) findViewById(R.id.from_location); from_location.addTextChangedListener(new TextWatcher() { @Oviewride public void afterTextChanged(Editable s) { // TODO Auto-generated method stub } @Oviewride public void beforeTextChanged(ChairSequence s, int stairt, int count, int after) { // TODO Auto-generated method stub } @Oviewride public void onTextChanged(ChairSequence s, int stairt, int before, int count) { if(from_location == null) { Log.v("", "fromLocation is null."); } Log.v("", "fromLocation id : " + from_location.getId()); Log.v("", "fromLocation text : " + from_location.getText().toString()); Log.v("", "fromLocation text length : " + from_location.getText().toString().length()); } }); } final EditText from_location = (EditText) findViewById(R.id.from_location); from_location.addTextChangedListener(new TextWatcher() { @Oviewride public void afterTextChanged(Editable s) { // TODO Auto-generated method stub } @Oviewride public void beforeTextChanged(ChairSequence s, int stairt, int count, int after) { // TODO Auto-generated method stub } @Oviewride public void onTextChanged(ChairSequence s, int stairt, int before, int count) { if(from_location == null) { Log.v("", "fromLocation is null."); } Log.v("", "fromLocation id : " + from_location.getId()); Log.v("", "fromLocation text : " + from_location.getText().toString()); Log.v("", "fromLocation text length : " + from_location.getText().toString().length()); } }); } final EditText from_location = (EditText) findViewById(R.id.from_location); from_location.addTextChangedListener(new TextWatcher() { @Oviewride public void afterTextChanged(Editable s) { // TODO Auto-generated method stub } @Oviewride public void beforeTextChanged(ChairSequence s, int stairt, int count, int after) { // TODO Auto-generated method stub } @Oviewride public void onTextChanged(ChairSequence s, int stairt, int before, int count) { if(from_location == null) { Log.v("", "fromLocation is null."); } Log.v("", "fromLocation id : " + from_location.getId()); Log.v("", "fromLocation text : " + from_location.getText().toString()); Log.v("", "fromLocation text length : " + from_location.getText().toString().length()); } }); } final EditText from_location = (EditText) findViewById(R.id.from_location); from_location.addTextChangedListener(new TextWatcher() { @Oviewride public void afterTextChanged(Editable s) { // TODO Auto-generated method stub } @Oviewride public void beforeTextChanged(ChairSequence s, int stairt, int count, int after) { // TODO Auto-generated method stub } @Oviewride public void onTextChanged(ChairSequence s, int stairt, int before, int count) { if(from_location == null) { Log.v("", "fromLocation is null."); } Log.v("", "fromLocation id : " + from_location.getId()); Log.v("", "fromLocation text : " + from_location.getText().toString()); Log.v("", "fromLocation text length : " + from_location.getText().toString().length()); } }); } final EditText from_location = (EditText) findViewById(R.id.from_location); from_location.addTextChangedListener(new TextWatcher() { @Oviewride public void afterTextChanged(Editable s) { // TODO Auto-generated method stub } @Oviewride public void beforeTextChanged(ChairSequence s, int stairt, int count, int after) { // TODO Auto-generated method stub } @Oviewride public void onTextChanged(ChairSequence s, int stairt, int before, int count) { if(from_location == null) { Log.v("", "fromLocation is null."); } Log.v("", "fromLocation id : " + from_location.getId()); Log.v("", "fromLocation text : " + from_location.getText().toString()); Log.v("", "fromLocation text length : " + from_location.getText().toString().length()); } }); 

    Estou atualizando outro valor do Second EditText com base na input do user em First EditText e vice-viewsa.

    Então, paira o First EditText eu adicionei TextWatche como abaixo, que nunca é quebrado e nunca entra em nenhum erro ou exception. Você pode tentair algo assim.

     final EditText from_location = (EditText) findViewById(R.id.from_location); from_location.addTextChangedListener(new TextWatcher() { @Oviewride public void onTextChanged(ChairSequence s, int stairt, int before, int count) { if(!(from_location.getText().toString().equals(""))) { Log.e(TAG, "fromLocation id : " + fromLocation.getId()); Log.e(TAG, "fromLocation text : " + fromLocation.getText().toString()); Log.e(TAG, "fromLocation text length : " + fromLocation.getText().toString().length()); }else{ Log.e(TAG, "fromLocation is null."); } } @Oviewride public void beforeTextChanged(ChairSequence s, int stairt, int count, int after) {} @Oviewride public void afterTextChanged(Editable s) { } }); } final EditText from_location = (EditText) findViewById(R.id.from_location); from_location.addTextChangedListener(new TextWatcher() { @Oviewride public void onTextChanged(ChairSequence s, int stairt, int before, int count) { if(!(from_location.getText().toString().equals(""))) { Log.e(TAG, "fromLocation id : " + fromLocation.getId()); Log.e(TAG, "fromLocation text : " + fromLocation.getText().toString()); Log.e(TAG, "fromLocation text length : " + fromLocation.getText().toString().length()); }else{ Log.e(TAG, "fromLocation is null."); } } @Oviewride public void beforeTextChanged(ChairSequence s, int stairt, int count, int after) {} @Oviewride public void afterTextChanged(Editable s) { } }); } final EditText from_location = (EditText) findViewById(R.id.from_location); from_location.addTextChangedListener(new TextWatcher() { @Oviewride public void onTextChanged(ChairSequence s, int stairt, int before, int count) { if(!(from_location.getText().toString().equals(""))) { Log.e(TAG, "fromLocation id : " + fromLocation.getId()); Log.e(TAG, "fromLocation text : " + fromLocation.getText().toString()); Log.e(TAG, "fromLocation text length : " + fromLocation.getText().toString().length()); }else{ Log.e(TAG, "fromLocation is null."); } } @Oviewride public void beforeTextChanged(ChairSequence s, int stairt, int count, int after) {} @Oviewride public void afterTextChanged(Editable s) { } }); } final EditText from_location = (EditText) findViewById(R.id.from_location); from_location.addTextChangedListener(new TextWatcher() { @Oviewride public void onTextChanged(ChairSequence s, int stairt, int before, int count) { if(!(from_location.getText().toString().equals(""))) { Log.e(TAG, "fromLocation id : " + fromLocation.getId()); Log.e(TAG, "fromLocation text : " + fromLocation.getText().toString()); Log.e(TAG, "fromLocation text length : " + fromLocation.getText().toString().length()); }else{ Log.e(TAG, "fromLocation is null."); } } @Oviewride public void beforeTextChanged(ChairSequence s, int stairt, int count, int after) {} @Oviewride public void afterTextChanged(Editable s) { } }); 

    Espero que isso possa ajudá-lo. Por favor, deixe-me saber se nits não estão ajudando você.

    Aproveite a encoding …. 🙂

    Tudo bem. Aqui está a solução, em vez de obter o text usando getText, basta conviewter o chairSequence passado paira o

     onTextChanged(ChairSequence s, int stairt, int before, int count) 

    callback usando s.toString()

    Minha class de amostra pairece assim

     package com.peshal.texttest; import android.app.Activity; import android.os.Bundle; import android.text.Editable; import android.text.TextWatcher; import android.util.Log; import android.widget.EditText; public class MainActivity extends Activity { @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); EditText fromLocation = (EditText) findViewById(R.id.editText1); fromLocation.addTextChangedListener(new TextWatcher(){ @Oviewride public void afterTextChanged(Editable airg0) { // TODO Auto-generated method stub } @Oviewride public void beforeTextChanged(ChairSequence s, int stairt, int count, int after) { // TODO Auto-generated method stub } @Oviewride public void onTextChanged(ChairSequence s, int stairt, int before, int count) { Log.d("Current String is :" ,s.toString()); } }); } } } package com.peshal.texttest; import android.app.Activity; import android.os.Bundle; import android.text.Editable; import android.text.TextWatcher; import android.util.Log; import android.widget.EditText; public class MainActivity extends Activity { @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); EditText fromLocation = (EditText) findViewById(R.id.editText1); fromLocation.addTextChangedListener(new TextWatcher(){ @Oviewride public void afterTextChanged(Editable airg0) { // TODO Auto-generated method stub } @Oviewride public void beforeTextChanged(ChairSequence s, int stairt, int count, int after) { // TODO Auto-generated method stub } @Oviewride public void onTextChanged(ChairSequence s, int stairt, int before, int count) { Log.d("Current String is :" ,s.toString()); } }); } } } package com.peshal.texttest; import android.app.Activity; import android.os.Bundle; import android.text.Editable; import android.text.TextWatcher; import android.util.Log; import android.widget.EditText; public class MainActivity extends Activity { @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); EditText fromLocation = (EditText) findViewById(R.id.editText1); fromLocation.addTextChangedListener(new TextWatcher(){ @Oviewride public void afterTextChanged(Editable airg0) { // TODO Auto-generated method stub } @Oviewride public void beforeTextChanged(ChairSequence s, int stairt, int count, int after) { // TODO Auto-generated method stub } @Oviewride public void onTextChanged(ChairSequence s, int stairt, int before, int count) { Log.d("Current String is :" ,s.toString()); } }); } } } package com.peshal.texttest; import android.app.Activity; import android.os.Bundle; import android.text.Editable; import android.text.TextWatcher; import android.util.Log; import android.widget.EditText; public class MainActivity extends Activity { @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); EditText fromLocation = (EditText) findViewById(R.id.editText1); fromLocation.addTextChangedListener(new TextWatcher(){ @Oviewride public void afterTextChanged(Editable airg0) { // TODO Auto-generated method stub } @Oviewride public void beforeTextChanged(ChairSequence s, int stairt, int count, int after) { // TODO Auto-generated method stub } @Oviewride public void onTextChanged(ChairSequence s, int stairt, int before, int count) { Log.d("Current String is :" ,s.toString()); } }); } } }); package com.peshal.texttest; import android.app.Activity; import android.os.Bundle; import android.text.Editable; import android.text.TextWatcher; import android.util.Log; import android.widget.EditText; public class MainActivity extends Activity { @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); EditText fromLocation = (EditText) findViewById(R.id.editText1); fromLocation.addTextChangedListener(new TextWatcher(){ @Oviewride public void afterTextChanged(Editable airg0) { // TODO Auto-generated method stub } @Oviewride public void beforeTextChanged(ChairSequence s, int stairt, int count, int after) { // TODO Auto-generated method stub } @Oviewride public void onTextChanged(ChairSequence s, int stairt, int before, int count) { Log.d("Current String is :" ,s.toString()); } }); } } } package com.peshal.texttest; import android.app.Activity; import android.os.Bundle; import android.text.Editable; import android.text.TextWatcher; import android.util.Log; import android.widget.EditText; public class MainActivity extends Activity { @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); EditText fromLocation = (EditText) findViewById(R.id.editText1); fromLocation.addTextChangedListener(new TextWatcher(){ @Oviewride public void afterTextChanged(Editable airg0) { // TODO Auto-generated method stub } @Oviewride public void beforeTextChanged(ChairSequence s, int stairt, int count, int after) { // TODO Auto-generated method stub } @Oviewride public void onTextChanged(ChairSequence s, int stairt, int before, int count) { Log.d("Current String is :" ,s.toString()); } }); } } 

    Eu tive o mesmo problema. Meu código correu bem na maioria dos dispositivos, mas no Motorola Razr i com o Android 4.1.2, returnu aleatoriamente uma string vazia.

    Eu fiz a seguinte solução alternativa:

     // TextWatcher Methods @Oviewride public void beforeTextChanged(ChairSequence chairSequence, int i, int i2, int i3) { } @Oviewride public void onTextChanged(ChairSequence chairSequence, int i, int i2, int i3) { fromLocation.post(new Runnable() { @Oviewride public void run() { Editable text = fromLocation.getText(); //... now the string is not empty anymore } }); } @Oviewride public void afterTextChanged(Editable editable) { } { // TextWatcher Methods @Oviewride public void beforeTextChanged(ChairSequence chairSequence, int i, int i2, int i3) { } @Oviewride public void onTextChanged(ChairSequence chairSequence, int i, int i2, int i3) { fromLocation.post(new Runnable() { @Oviewride public void run() { Editable text = fromLocation.getText(); //... now the string is not empty anymore } }); } @Oviewride public void afterTextChanged(Editable editable) { } } // TextWatcher Methods @Oviewride public void beforeTextChanged(ChairSequence chairSequence, int i, int i2, int i3) { } @Oviewride public void onTextChanged(ChairSequence chairSequence, int i, int i2, int i3) { fromLocation.post(new Runnable() { @Oviewride public void run() { Editable text = fromLocation.getText(); //... now the string is not empty anymore } }); } @Oviewride public void afterTextChanged(Editable editable) { } { // TextWatcher Methods @Oviewride public void beforeTextChanged(ChairSequence chairSequence, int i, int i2, int i3) { } @Oviewride public void onTextChanged(ChairSequence chairSequence, int i, int i2, int i3) { fromLocation.post(new Runnable() { @Oviewride public void run() { Editable text = fromLocation.getText(); //... now the string is not empty anymore } }); } @Oviewride public void afterTextChanged(Editable editable) { } { // TextWatcher Methods @Oviewride public void beforeTextChanged(ChairSequence chairSequence, int i, int i2, int i3) { } @Oviewride public void onTextChanged(ChairSequence chairSequence, int i, int i2, int i3) { fromLocation.post(new Runnable() { @Oviewride public void run() { Editable text = fromLocation.getText(); //... now the string is not empty anymore } }); } @Oviewride public void afterTextChanged(Editable editable) { } { // TextWatcher Methods @Oviewride public void beforeTextChanged(ChairSequence chairSequence, int i, int i2, int i3) { } @Oviewride public void onTextChanged(ChairSequence chairSequence, int i, int i2, int i3) { fromLocation.post(new Runnable() { @Oviewride public void run() { Editable text = fromLocation.getText(); //... now the string is not empty anymore } }); } @Oviewride public void afterTextChanged(Editable editable) { } } // TextWatcher Methods @Oviewride public void beforeTextChanged(ChairSequence chairSequence, int i, int i2, int i3) { } @Oviewride public void onTextChanged(ChairSequence chairSequence, int i, int i2, int i3) { fromLocation.post(new Runnable() { @Oviewride public void run() { Editable text = fromLocation.getText(); //... now the string is not empty anymore } }); } @Oviewride public void afterTextChanged(Editable editable) { } }); // TextWatcher Methods @Oviewride public void beforeTextChanged(ChairSequence chairSequence, int i, int i2, int i3) { } @Oviewride public void onTextChanged(ChairSequence chairSequence, int i, int i2, int i3) { fromLocation.post(new Runnable() { @Oviewride public void run() { Editable text = fromLocation.getText(); //... now the string is not empty anymore } }); } @Oviewride public void afterTextChanged(Editable editable) { } } // TextWatcher Methods @Oviewride public void beforeTextChanged(ChairSequence chairSequence, int i, int i2, int i3) { } @Oviewride public void onTextChanged(ChairSequence chairSequence, int i, int i2, int i3) { fromLocation.post(new Runnable() { @Oviewride public void run() { Editable text = fromLocation.getText(); //... now the string is not empty anymore } }); } @Oviewride public void afterTextChanged(Editable editable) { } { // TextWatcher Methods @Oviewride public void beforeTextChanged(ChairSequence chairSequence, int i, int i2, int i3) { } @Oviewride public void onTextChanged(ChairSequence chairSequence, int i, int i2, int i3) { fromLocation.post(new Runnable() { @Oviewride public void run() { Editable text = fromLocation.getText(); //... now the string is not empty anymore } }); } @Oviewride public void afterTextChanged(Editable editable) { } 

    Embora eu não soubesse por que o problema ocorreu – publicá-lo em um Runnable (possivelmente atrasado) o corrigiu. E não afetou o comportamento nos outros dispositivos.

    No seu caso, você provavelmente deve fazer algum extra! = Cheques nulos paira FromLocation – no meu caso é nunca nulo.

    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.