我有一个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 listener,同样的结果。
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);
}
我做错了什么? 提前谢谢!
最终解决方案: 看到自己的答案。
答案 0 :(得分:0)
只需将updateAll
方法移至onSharedPreferencesChanged()
即可。
@Override public void onSharedPreferenceChanged (SharedPreferences sharedPreferences, String key) {
updateAll (sharedPreferences);
}
答案 1 :(得分:0)
因为它似乎与updateAll()
方法无关,所以我决定抓住IndexOutOfBounds
异常,解决了这个问题。
@Override
public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
try {
// updating and stuff
} catch (IndexOutOfBoundsException e) {
e.printStackTrace();
}
}