All over the net I see examples like edittext.getText().toString(). I do not see any null check. In docs I do not see any statement that would say that this will never be null.
Still, what does the observations say; does this ever return null?
getText() will not return null. So there is no chance for NPE in following method. the getText will return empty string if there is no string, which is definitely not null
getText().toString();
However the edittext itself can be null if not initialized properly, Hence the following will trigger NPE
editText.getText().toString();
UPDATE:
It does appear as of Jan 18, 2018 this is now possible.
OLD ANSWER:
No, EditText.getText() never returns null. One way to verify this is to check the Android source code for EditText.getText():
EditText.java shows:
public Editable getText() {
return (Editable) super.getText();
}
Since EditText extends TextView, the call to super.getText() must be TextView.getText(). Now we move on to TextView.getText() to see what it returns:
TextView.java shows:
public CharSequence getText() {
return mText;
}
Now we need to know if mText could ever be null.
Digging deeper into the TextView.java source we see that mText is initialized as an empty string in the TextView constructor:
public TextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
mText = "";
…
}
Once we see that the EditText constructor calls the TextView constructor:
public EditText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
we can safely conclude that EditText.getText() can never return null, because as soon as an EditText is constructed, mText is given a value of an empty string.
However, as StinePike pointed out, EditText.getText() can possibly cause an NPE if your EditText is null when it makes the call to getText().
With Android P SDK it is annotated as nullable in the AppCompatEditText class so it can return null.
And from the docs:
Return the text that the view is displaying. If an editable text has not been set yet, this will return null.
#Override
#Nullable
public Editable getText() {
if (Build.VERSION.SDK_INT >= 28) {
return super.getText();
}
// A bug pre-P makes getText() crash if called before the first setText due to a cast, so
// retrieve the editable text.
return super.getEditableText();
}
I dont think so it will ever return null.
But if you want to check whether the returned text is empty or not might I suggest using TextUtils.isEmpty() method
Edit:- The documentation doesn't states anything regarding the returned value. And from what I've seen in the source code is that when you initialize a EditText, the default text value is set to "". So it will never return null
it will return null because when apps runs its empty and it returns null, use .getText.toString inside a button click listener, now when you click button it will get the text which you have entered on editText.
it is just because your edittext.getText().toString() is called directly in onCreate()...
This is why it return the initial value of editText.
You just have to create a function out of onCreate() and call it in event listener.
onCreate (){
….............
btn = (Button)findViewById(R.id.firstButton);
btn.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
getValue ()
}
});
}
public void getValue () {
String editTextValue= edittext.getText().toString();
......
}
Or you Can call it directly in you onClickListenner in onCreate().
Like this:
onCreate (){
….............
btn = (Button)findViewById(R.id.firstButton);
btn.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
String editTextValue= edittext.getText().toString();
.....
}
});
}
I Hope it will be helpful!
try in this way
String edittext = edittext.getText().toString();
if(edittext.length==0){ Log.d("null","the valueis null")};
Related
Why can't i access the text from edittext via gettext
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(name.getText().toString() != "") {
}
It says name has to be declared final. if so i cannot run the programm. Click doesn't work or application stops.
Declare your EditText and Button as global and initialize it
In java, when using == on two objects, you're not actually comparing the strings themselves. You'll need to use .equals(String) or .equalignorecase(String).
== actually compares the two object's references, not their values.
string1.equals(String target) compares the two strings based off of the actual characters in the strings.
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(name.getText().toString().equalsIgnoreCase("")) {
}
It has to be declared final inside the onCreate method, not above the onCreate mothed to get rid of all errors.
Either declare your edittext as final or as global.
Moreover, you should never use != for string comparison. It is prone to error. Change :
if(name.getText().toString() != "")
to :
if(!name.getText().toString().equals(""))
or in this specific case since what you actually care about is the string not being empty, you can use :
if(!name.getText().toString().isEmpty())
I have a big problem with the SharedPreferences in Android. The preferences are adding unwanted chars to one of my string values once the application is closed. Actually it is a configurable escape sequence.
I have a simple setup, a MainActivity containing
#Override
protected void onStart() {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
sequence = prefs.getString("escape_sequence", "");
}
And a preferences screen where the value is set. When i open the app go to the prefences screen, set the value correctly to \n\n and go back to the MainActivity the breakpoint is correctly displaying the sequence as Java.lang.String, value(char[2])= "\n\n", count=2. When i am now restarting the app through android studio the same breakpoint in the code suddenly displays: Java.lang.String, value(char[6])= "\n\n ", count=6, containing 4 Space and 10 escape \u0000 characters.
Can anybody why this is happening and what i can do about that?
BTW i'm not touching the SharedPreferences.Editor anywhere in the App so far. I is strictly done via the PreferencesScreen. So no overwrite is done anywhere in the app. The default values shouldn't be applied either, however the setting is android:defaultValue="\n\n" anyway.
EDIT:
I found the reason: android adds the spaces if a newline is at the end of the preference. I have no idea why.
EDIT:
Here is the custom preference code:
public class SequencePreference extends DialogPreference {
EditText sequenceInput;
public SequencePreference(Context context, AttributeSet attrs) {
super(context, attrs);
setDialogLayoutResource(R.layout.dialog_preference_sequence);
setPositiveButtonText(R.string.settings_sequence_ok);
setNegativeButtonText(R.string.settings_sequence_cancel);
setDialogIcon(null);
}
#Override
protected View onCreateDialogView() {
View view = super.onCreateDialogView();
sequenceInput= (EditText)view.findViewById(R.id.sequence_input);
return view;
}
#Override
protected void onDialogClosed(boolean positiveResult) {
// When the user selects "OK", persist the new value
if (positiveResult) {
String sequenceValue = new String( sequenceInput.getText().toString() );
String[] parts = sequenceValue.split("-");
if(parts.length == 2) {
persistString(parts[1]);
}
}
}
}
I think this is a bug in Android API 18 and newer where extra whitespace is injected when a SharedPreferences string ends with \n. For more information, see:
https://code.google.com/p/android/issues/detail?id=159799#c6
https://code.google.com/p/android/issues/detail?id=159799#c7
after you retrieve the saved string, place a trim().
example
String sequence2 = sequence.trim()
I have a little problem with checking length of EditText.
text = take.getText().toString();
if (text.matches("")) {
Toast.makeText(getApplicationContext(), "Enter first value", Toast.LENGTH_SHORT).show();
take is a EditText and text is a String.
When i run my app without Edittext value i got an Error:
Unfortunately "ApplicationName" has stopped.
In my Class i have
private EditText take;
private String text;
Please help me, i try to use method .equals("") or .length()==0 but stil the same.
you may have forgotten findViewById or you are calling it before setContentView of your activity!
Some time match and equal will not return proper. I prefer to use a function to check empty value.
public static String chechNullState(EditText checkTextBox) {
String makeModVal = null;
if (!TextUtils.isEmpty( checkTextBox.getText().toString() ))
{
makeModVal=checkTextBox.getText().toString();
return makeModVal;
}
else
{
return null;
}
}
Usage:
item =(EditText)findViewById(R.id.take);
String takeVal = chechNullState(item);
if ( !takeVal == null)
{
//do what ever you want;
}
I'm writing custom EditTextPreference.
Using this code inside my CustomEditTextPreference:
#Override
protected void onDialogClosed(boolean shouldSave) {
if (shouldSave) {
String sValue = getText();
value = Float.parseFloat(sValue);
peristValue();
}
}
sValue is null. How do I acquire the value from edit then?
You should probably use
getEditText().getText().toString();
Since getText() by itself gets the current SharedPreference value, which may or may not exist.
Is there a way to create a preference in a PreferenceFragment that accepts only integer values? I could implement an EditTextPreference and register an OnPreferenceChangeListener in which I could reject the change if the user enters a a string that is not a number, but I would prefer something that is meant for holding only numbers and that does not allow users to enter anything else, maybe showing only a dial pad keyboard.. I don't such a preference exist, since every descendant of Preference is mapped onto a Boolean (CheckBoxPreference), a String (*EditTextPreference) or a String array (MultiSelectListPreference), i.e. there are no preferences mapped onto integers, but maybe some of you can give me an hint or at least tell me if there are better solutions than the one I've proposed above.
Solution proposed by Grey:
EditText editText = ((EditTextPreference)
findPreference("intent_property")).getEditText();
editText.setKeyListener(new NumberKeyListener() {
#Override
public int getInputType() {
// The following shows the standard keyboard but switches to the view
// with numbers on available on the top line of chars
return InputType.TYPE_CLASS_NUMBER;
// Return the following to show a dialpad as the one shown when entering phone
// numbers.
// return InputType.TYPE_CLASS_PHONE
}
#Override
protected char[] getAcceptedChars() {
return new String("1234567890").toCharArray();
}
});
Shorter solution, which does not allow varying the keyboard to dialpad but requires less code:
EditText editText = ((EditTextPreference)
findPreference("intent_property")).getEditText();
editText.setKeyListener(new DigitsKeyListener());
I don't like just one thing about this solution: the user can enter 0000 and it is accepted and saved in the shared preference (which is a String) as "0000".. this requires you to implement a OnSharedPreferenceChangeListener to intercept changes to shared preferences and remove leading zeros in this preference or implement a change listener directly on this preference and return false to refuse numbers with trailing zeros (tell me if there is a better solution to this last problem which does not involve implementing your own Preference). It would be beautiful if we could modify the newValue in OnPreferenceChangeListener..
There is an easier way to do this. Just set in the EditTextPreference in your XML to android:numeric="integer" (you can set it to integer, signed or decimal).
Eclipse (or whatever tool you are working in) won't show you this as a possible attribute, but just write it in your EditTextPreference and clean your project. You will see that it won't give you any errors. :)
After reading the proposed solutions, I still think a custom preference is the way to go. I did read your remark in bold text, but setting up a basic EditTextIntegerPreference is actually super simple and it will solve the remaining issue you have when the user enters for example "0000".
Just a note up front: since you normally want to be able to use preferences in several places in your app, as far as I'm concerned, a proper implementation of an EditTextIntegerPreference would store its value against an Integer. That way you'll be able to retrieve the int value anywhere, without the need to first parse or cast it.
However, to keep this answer to the point and compact, I'm actually going to show an extension of a regular EditTextPreference, which means that under the hood, the value will still be stored as a string. If you're really keen on getting the 'proper' implementation to work, I don't mind writing that up later on. It shouldn't be too tricky though, so you might want to have a crack at it yourself first. :)
public class EditTextIntegerPreference extends EditTextPreference {
private Integer mInteger;
public EditTextIntegerPreference(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
getEditText().setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_SIGNED);
}
public EditTextIntegerPreference(Context context, AttributeSet attrs) {
super(context, attrs);
getEditText().setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_SIGNED);
}
public EditTextIntegerPreference(Context context) {
super(context);
getEditText().setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_SIGNED);
}
#Override public void setText(String text) {
final boolean wasBlocking = shouldDisableDependents();
mInteger = parseInteger(text);
persistString(mInteger != null ? mInteger.toString() : null);
final boolean isBlocking = shouldDisableDependents();
if (isBlocking != wasBlocking) notifyDependencyChange(isBlocking);
}
#Override public String getText() {
return mInteger != null ? mInteger.toString() : null;
}
private static Integer parseInteger(String text) {
try { return Integer.parseInt(text); }
catch (NumberFormatException e) { return null; }
}
}
The reason why this solves the "0000" issue, is that we're simply casting the user typed value to an int before we store it, and plug it back into a string upon retrieval. That means any leading zeroes (except for '0' as a number of course) will automagically disappear.
The 'number' input type will restrict the user to input only enter numbers, so the parseInteger(...) method is mainly present for sanity reasons, although it will catch a NumberFormatException if you try to enter an empty or null string. Again, you can make this more pretty, which I haven't done for now.
i guess u can call getEditText() to get a EditText obj , then call setKeyListener with a NumberKeyListener.
You should use PreferenceActivity which is basically used to show preferences like an xml file. In this activity you can use checkboxes, edit text and save values to preferences.
You can also reopen the dialog when you get an exception after trying to parseInt() the number.
Here is a sample code:
public class MyEditTextPreference extends EditTextPreference {
private EditText _editText;
public MyEditTextPreference(Context context, AttributeSet attrs) {
super(context, attrs);
_editText = getEditText();
}
#Override
public void onDismiss(DialogInterface dialog) {
super.onDismiss(dialog);
try {
Integer.parseInt(_editText.getText().toString());
} catch (NumberFormatException e) {
((Dialog) dialog).show();
}
}
}