I have limited the value in this EditText field to numbers, and max length of 3
main.xml
<EditText android:id="@+id/inputTime" android:layout_width="60sp"
android:layout_height="wrap_content" android:text="@string/inputMaxTime"
android:textSize="15sp" android:digits="0123456789" android:inputType="numberDecimal"
android:numeric="decimal" android:maxLength="3">
</EditText>
I am also, programmatically, limiting the value to be 120, and this is the code:
final EditText inputMinutes = (EditText) findViewById(R.id.inputTime);
inputMinutes.setText(String.valueOf(preferences.getLong("neglectedPeriodMsecs", 60000)/60000));
inputMinutes.addTextChangedListener(new TextWatcher()
{
public void onTextChanged(CharSequence s, int start, int before, int count)
{
}
public void beforeTextChanged(CharSequence s, int start, int count, int after)
{
}
@Override
public void afterTextChanged(Editable s)
{
// TODO Auto-generated method stub
if(isFreeVersion && Integer.valueOf(inputMinutes.getText().toString()) > 120)
showToast("The free version is limited to 120 minutes.");
else
{
editor.putLong("neglectedPeriodMsecs", Long.parseLong(s.toString())*60000);
editor.commit();
}
}
});
I also tried to add another check on the else
else if (Integer.valueOf(inputMinutes.getText().toString()) > 0)
{
editor.putLong("neglectedPeriodMsecs", Long.parseLong(s.toString())*60000);
editor.commit();
}
no change in the result.
The problem is that when the user deletes ALL the numbers from the view, I get an error. I think I can use a try/catch, but not sure which one, and if that’s the correct way.
Thanks
PFA Logcat
06-27 23:16:00.014: VERBOSE/AlarmManager(2489): set: Alarm{48248e28 type 1 android}
06-27 23:16:42.998: DEBUG/AndroidRuntime(30567): Shutting down VM
06-27 23:16:42.998: WARN/dalvikvm(30567): threadid=1: thread exiting with uncaught exception (group=0x4001d7d0)
06-27 23:16:43.264: ERROR/AndroidRuntime(30567): FATAL EXCEPTION: main
06-27 23:16:43.264: ERROR/AndroidRuntime(30567): java.lang.NumberFormatException: unable to parse ” as integer
06-27 23:16:43.264: ERROR/AndroidRuntime(30567): at java.lang.Integer.parseInt(Integer.java:412)
06-27 23:16:43.264: ERROR/AndroidRuntime(30567): at java.lang.Integer.parseInt(Integer.java:382)
06-27 23:16:43.264: ERROR/AndroidRuntime(30567): at java.lang.Integer.valueOf(Integer.java:682)
06-27 23:16:43.264: ERROR/AndroidRuntime(30567): at com.neglected.Neglected$2.afterTextChanged(Neglected.java:120)
06-27 23:16:43.264: ERROR/AndroidRuntime(30567): at android.widget.TextView.sendAfterTextChanged(TextView.java:6339)
06-27 23:16:43.264: ERROR/AndroidRuntime(30567): at android.widget.TextView$ChangeWatcher.afterTextChanged(TextView.java:6522)
06-27 23:16:43.264: ERROR/AndroidRuntime(30567): at android.text.SpannableStringBuilder.sendTextHasChanged(SpannableStringBuilder.java:897)
06-27 23:16:43.264: ERROR/AndroidRuntime(30567): at android.text.SpannableStringBuilder.change(SpannableStringBuilder.java:401)
06-27 23:16:43.264: ERROR/AndroidRuntime(30567): at android.text.SpannableStringBuilder.change(SpannableStringBuilder.java:269)
06-27 23:16:43.264: ERROR/AndroidRuntime(30567): at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:432)
06-27 23:16:43.264: ERROR/AndroidRuntime(30567): at android.text.SpannableStringBuilder.delete(SpannableStringBuilder.java:218)
06-27 23:16:43.264: ERROR/AndroidRuntime(30567): at android.text.SpannableStringBuilder.delete(SpannableStringBuilder.java:28)
06-27 23:16:43.264: ERROR/AndroidRuntime(30567): at android.text.method.BaseKeyListener.backspace(BaseKeyListener.java:60)
06-27 23:16:43.264: ERROR/AndroidRuntime(30567): at android.text.method.BaseKeyListener.onKeyDown(BaseKeyListener.java:123)
06-27 23:16:43.264: ERROR/AndroidRuntime(30567): at android.text.method.NumberKeyListener.onKeyDown(NumberKeyListener.java:134)
06-27 23:16:43.264: ERROR/AndroidRuntime(30567): at android.widget.TextView.doKeyDown(TextView.java:4479)
06-27 23:16:43.264: ERROR/AndroidRuntime(30567): at android.widget.TextView.onKeyDown(TextView.java:4324)
06-27 23:16:43.264: ERROR/AndroidRuntime(30567): at android.view.KeyEvent.dispatch(KeyEvent.java:1124)
06-27 23:16:43.264: ERROR/AndroidRuntime(30567): at android.view.View.dispatchKeyEvent(View.java:3740)
06-27 23:16:43.264: ERROR/AndroidRuntime(30567): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:788)
06-27 23:16:43.264: ERROR/AndroidRuntime(30567): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:788)
06-27 23:16:43.264: ERROR/AndroidRuntime(30567): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:788)
06-27 23:16:43.264: ERROR/AndroidRuntime(30567): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:788)
06-27 23:16:43.264: ERROR/AndroidRuntime(30567): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchKeyEvent(PhoneWindow.java:1883)
06-27 23:16:43.264: ERROR/AndroidRuntime(30567): at com.android.internal.policy.impl.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1154)
06-27 23:16:43.264: ERROR/AndroidRuntime(30567): at android.app.Activity.dispatchKeyEvent(Activity.java:2063)
06-27 23:16:43.264: ERROR/AndroidRuntime(30567): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1859)
06-27 23:16:43.264: ERROR/AndroidRuntime(30567): at android.view.ViewRoot.deliverKeyEventToViewHierarchy(ViewRoot.java:2495)
06-27 23:16:43.264: ERROR/AndroidRuntime(30567): at android.view.ViewRoot.handleMessage(ViewRoot.java:1985)
06-27 23:16:43.264: ERROR/AndroidRuntime(30567): at android.os.Handler.dispatchMessage(Handler.java:99)
06-27 23:16:43.264: ERROR/AndroidRuntime(30567): at android.os.Looper.loop(Looper.java:123)
06-27 23:16:43.264: ERROR/AndroidRuntime(30567): at android.app.ActivityThread.main(ActivityThread.java:4627)
06-27 23:16:43.264: ERROR/AndroidRuntime(30567): at java.lang.reflect.Method.invokeNative(Native Method)
06-27 23:16:43.264: ERROR/AndroidRuntime(30567): at java.lang.reflect.Method.invoke(Method.java:521)
06-27 23:16:43.264: ERROR/AndroidRuntime(30567): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
06-27 23:16:43.264: ERROR/AndroidRuntime(30567): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
06-27 23:16:43.264: ERROR/AndroidRuntime(30567): at dalvik.system.NativeStart.main(Native Method)
06-27 23:16:43.350: WARN/ActivityManager(2489): Process com.neglected has crashed too many times: killing!
06-27 23:16:43.350: WARN/ActivityManager(2489): Force finishing activity com.neglected/.Neglected
06-27 23:16:43.358: INFO/Process(2489): Sending signal. PID: 30567 SIG: 9
06-27 23:16:43.381: INFO/WindowManager(2489): WIN DEATH: Window{47f27e88 com.neglected/com.neglected.Neglected paused=true}
06-27 23:16:53.352: WARN/ActivityManager(2489): Launch timeout has expired, giving up wake lock!
Solution:
Changing the if to:
public void afterTextChanged(Editable s)
{
String minValueStr = inputMinutes.getText().toString();
if (minValueStr.length() != 0 ) {
int minValueInt = Integer.valueOf(minValueStr);
if(isFreeVersion && minValueInt > FREE_VERSION_MAX_NEGLECTED_TIME)
{
showToast("The free version is limited to " + String.valueOf(FREE_VERSION_MAX_NEGLECTED_TIME) + " minutes.");
inputMinutes.setText(String.valueOf(FREE_VERSION_MAX_NEGLECTED_TIME));
}
else if (minValueInt > 0)
{
editor.putLong("neglectedPeriodMsecs", Long.parseLong(s.toString())*60000);
editor.commit();
}
}
}
thanks for the suggestions. they helped me keep looking.
Well I would guess that is being caused by a null pointer exception or a exception due to parsing since you are trying to convert the string in the edittext field to a number. So when the user clears the textfield, the code tries to change the null value or empty string into a number which is not possible.
I am not saying that this is the right answer but this is what I thought after seeing your code.
You can programatically check to see if the textfield is null and then write the appropriate logic when the textfield is set to null/empty string.
If it is a nullpointer exception then you can catch that using a try catch block.
If these are not true then it will be helpful if you post the stacktrace from your logcat which shows the error log so that others can see whats causing this.