我有一个 PreferenceFragment 活动,它显示以下 EditTextPreference:
<EditTextPreference
android:key="year"
android:title="@string/prefs_yearPicker"
android:summary="@string/prefs_yearPickerSummary"
android:textAllCaps="true"/>
但是当我想更改值时,它没有显示光标,当我按 OK 时,它崩溃,返回以下消息:
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)
偏好类代码:
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);
}
}
我已经尝试过没有 on change 监听器,结果相同。
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);
}
我究竟做错了什么?提前致谢!
最终解决方案:查看自己的答案。