EditTextPreference crashes on OK press

281 views Asked by At

I have a PreferenceFragment activity which shows the following EditTextPreference:

<EditTextPreference
            android:key="year"
            android:title="@string/prefs_yearPicker"
            android:summary="@string/prefs_yearPickerSummary"
            android:textAllCaps="true"/>

But when I want to change the value, it shows no cursor, and when I press OK, it crashes, returning the following message:

11-27 07:59:33.900: E/AndroidRuntime(23355): FATAL EXCEPTION: main
11-27 07:59:33.900: E/AndroidRuntime(23355): java.lang.IndexOutOfBoundsException: replace (0 ... -1) has end before start
11-27 07:59:33.900: E/AndroidRuntime(23355):    at android.text.SpannableStringBuilder.checkRange(SpannableStringBuilder.java:1009)
11-27 07:59:33.900: E/AndroidRuntime(23355):    at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:441)
11-27 07:59:33.900: E/AndroidRuntime(23355):    at android.text.SpannableStringBuilder.delete(SpannableStringBuilder.java:212)
11-27 07:59:33.900: E/AndroidRuntime(23355):    at android.text.SpannableStringBuilder.delete(SpannableStringBuilder.java:30)
11-27 07:59:33.900: E/AndroidRuntime(23355):    at android.view.inputmethod.BaseInputConnection.deleteSurroundingText(BaseInputConnection.java:242)
11-27 07:59:33.900: E/AndroidRuntime(23355):    at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:382)
11-27 07:59:33.900: E/AndroidRuntime(23355):    at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:77)
11-27 07:59:33.900: E/AndroidRuntime(23355):    at android.os.Handler.dispatchMessage(Handler.java:99)
11-27 07:59:33.900: E/AndroidRuntime(23355):    at android.os.Looper.loop(Looper.java:137)
11-27 07:59:33.900: E/AndroidRuntime(23355):    at android.app.ActivityThread.main(ActivityThread.java:5227)
11-27 07:59:33.900: E/AndroidRuntime(23355):    at java.lang.reflect.Method.invokeNative(Native Method)
11-27 07:59:33.900: E/AndroidRuntime(23355):    at java.lang.reflect.Method.invoke(Method.java:511)
11-27 07:59:33.900: E/AndroidRuntime(23355):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
11-27 07:59:33.900: E/AndroidRuntime(23355):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
11-27 07:59:33.900: E/AndroidRuntime(23355):    at dalvik.system.NativeStart.main(Native Method)

Preferences class code:

public class Preferences extends ActionBarActivity  {

    final Context context = getBaseContext();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // set the layout
        setContentView(R.layout.preferences_layout);

        // display the fragment as the main content.
        getFragmentManager().beginTransaction()
                .replace(R.id.preferences, new SettingsFragment())
                .commit();

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        if (toolbar != null) {
            setSupportActionBar(toolbar);
        }

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        }
    }
}

public static class SettingsFragment extends PreferenceFragment implements OnSharedPreferenceChangeListener {
        @Override
        public void onCreate(Bundle paramBundle) {

                super.onCreate(paramBundle);
                addPreferencesFromResource(R.xml.preferences);

                // get shared preferences
                SharedPreferences prefs = getPreferenceScreen().getSharedPreferences();
                // set on change listener
                prefs.registerOnSharedPreferenceChangeListener(this);
                // update all summaries
                updateAll(prefs);

            }
}

I've tried it without the on change listener, same result.

void updateAll(SharedPreferences prefs) {

    // set the summary
    EditTextPreference editTextPref = (EditTextPreference) findPreference("year");
    editTextPref.setSummary(prefs.getString("year", ""));

    // set the summary of interval
    ListPreference listPref = (ListPreference) findPreference("notificationsInterval");
    String intervalValue = prefs.getString("notificationsInterval", "1800000");
    String text = msToH(Integer.valueOf(intervalValue));
    listPref.setSummary(text);

}

What am I doing wrong? Thanks in advance!

FINAL SOLUTION: See own answer.

2

There are 2 answers

0
niels On BEST ANSWER

Because it didn't appear to have anything to do with the updateAll() method, I decided to catch the IndexOutOfBounds exception, which solved the problem.

@Override
public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
    try {
        // updating and stuff
    } catch (IndexOutOfBoundsException e) {
        e.printStackTrace();
    }
}
0
Murtaza Khursheed Hussain On

Just move your updateAll method to onSharedPreferencesChanged().

@Override public void onSharedPreferenceChanged (SharedPreferences sharedPreferences, String key) {

       updateAll (sharedPreferences); 
}