EditText.getText (). ToString () sometimes returns a ""

I get the text EditText and its length in the onTextChanged() method of the TextWatcher .
It works fine when I print to add characters, but when removing characters from text, getText() gives an empty one, even if the text is not empty. This happens randomly, and not every time I delete characters. I observed that this happens mainly when there are 3-4 characters in the text, and I press backspace.

The strange part is that this problem only occurs on the device, and not on the emulator.

Layout File:

 <LinearLayout style="@style/create_trip_activity_components_style" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="30dp" android:gravity="center_vertical" android:orientation="horizontal" > <EditText android:id="@+id/from_location" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="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_parent" android:layout_height="wrap_content" android:layout_gravity="right" android:onClick="useCurrentLocation" android:text="@string/use_current" android:textAppearance="?android:attr/textAppearanceSmall" /> </LinearLayout> 

The code:

 fromLocation.addTextChangedListener(new TextWatcher() { @Override public void afterTextChanged(Editable s) { } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void onTextChanged(CharSequence s, int start, 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()); } }); 

Note. . I tried using the afterTextChanged() and beforeTextChanged() methods. But this did not solve the problem.

+7
android null android-edittext gettext
source share
10 answers

I do not see any problems. The text from_location should change and this will happen.

Why do you "check" EditText ( from_location ) when you are inside TextWatcher code. I do not think that the value of EditText should be โ€œstableโ€ when changing.

Perhaps it happens that when checking for an update, CharSequence in from_location happens, and sometimes you break it only in the middle of the change, sometimes after.

Have you checked if (CharSequence s, int start, int before, int count) returns the expected values?

As I see this situation. If you want to make any changes to the text, you must make them in the String s argument of the afterTextChanged method.

I wrote this code to check what happens by modifying the contents of an EditText , maybe this is any

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

If you check the source code of EditText , you can see that the getText method is getText from TextView and it returns a CharSequence mText . You can sow another CharSequence mTransformed . And this is in the huge setText method there is a moment when mText is replaced by mTransformed . Probably when you call from_location getText , you get mText , and the second time you do it after setText , and you get mTransformed response.

If you want to check it out just change

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

If you went through debugging mode, keep in mind that there are two places where values โ€‹โ€‹are stored. In my case, EditText had mText (Spannable String Builder), and its subfield (also mText) is identifier with identifier. The released string builder returns the value .toString (). Another returns the value .getText ().

0
source share

Try using CharSequence s from onTextChange () so that it is the same as the one returned fromLocation.getText ().

0
source share
 public void afterTextChanged(Editable s) { if(s.length()>=6) { } else{ } } public void beforeTextChanged(CharSequence s, int start, int count, int after) { } public void onTextChanged(CharSequence s, int start, int before, int count) { } 
0
source share

This may be what you need. This is the code that I used in the project to search by strings. I have a method called mySearchMethod, don't worry about that. But I get a text line editing method after changing the text.

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

Call the search () method in your create file to initialize it.

0
source share

Which of the list? You should use CharSequence as shown below.

Try it......

 @Override public void onTextChanged(CharSequence s, int start, 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()); } 
0
source share

Try it, it will work ...

 final EditText from_location = (EditText) findViewById(R.id.from_location); from_location.addTextChangedListener(new TextWatcher() { @Override public void afterTextChanged(Editable s) { // TODO Auto-generated method stub } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { // TODO Auto-generated method stub } @Override public void onTextChanged(CharSequence s, int start, 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()); } }); 
0
source share

I am updating another second EditText value based on user input in First EditText and vice versa.

So, for First EditText, I added a TextWatche, as shown below, which never crashes and never gets into any error or exception. You can try something like that.

 final EditText from_location = (EditText) findViewById(R.id.from_location); from_location.addTextChangedListener(new TextWatcher() { @Override public void onTextChanged(CharSequence s, int start, 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."); } } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) {} @Override public void afterTextChanged(Editable s) { } }); 

Hope this helps you. Please let me know if the nits do not help you.

Enjoy the coding .... :)

0
source share

Alright Here is a solution, instead of getting text using getText, just convert the charSequence passed to

 onTextChanged(CharSequence s, int start, int before, int count) 

using s.toString()

My sample class is as follows

 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 { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); EditText fromLocation = (EditText) findViewById(R.id.editText1); fromLocation.addTextChangedListener(new TextWatcher(){ @Override public void afterTextChanged(Editable arg0) { // TODO Auto-generated method stub } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { // TODO Auto-generated method stub } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { Log.d("Current String is :" ,s.toString()); } }); } } 
0
source share

I had the same problem. My code worked fine on most devices, but on Motorola Razr with Android 4.1.2 I accidentally returned an empty string.

I made the following workaround:

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

Although I did not know why the problem arose - placing it in Runnable (possibly with a delay) fixed it. And it did not affect the behavior of other devices.

In your case, you probably have to do something extra! = Null checks for fromLocation - in my case it was never null.

0
source share

All Articles