问题
I am trying to create a custom time picker. But the problem that ran into is that when i rotate the screen. also if I press the home button to leave the app it crashes when i come back with the same error.
public class MyTimePicker extends RelativeLayout {
private NumberPicker hour;
private NumberPicker minute;
private NumberPicker am;
private int interval = 5;
public MyTimePicker(Context context) {
super(context);
init();}
public MyTimePicker(Context context, AttributeSet attrs) {
super(context, attrs);
init();}
public MyTimePicker(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();}
private void init() {
LayoutInflater inflater = LayoutInflater.from(getContext());
if (inflater != null) {
String[] ams = new String[2];
ams[0]="AM";
ams[1]="PM";
root = inflater.inflate(R.layout.my_time_picker, this, true);
hour = (NumberPicker) root.findViewById(R.id.hour);
minute = (NumberPicker) root.findViewById(R.id.minute);
am = (NumberPicker) root.findViewById(R.id.am); am.setDisplayedValues(ams);
am.setWrapSelectorWheel(true);
am.setMinValue(0);
am.setMaxValue(1);
String[] hours = new String[24];
for (int i = 0; i < 24; i++) {
hours[i]= "" + (i < 12 ? i + 1 : i - 11);
}
String[] mins = new String[60 / interval];
NumberFormat formatter = new DecimalFormat("00");
for (int i = 0; i < mins.length; i++) {
mins[i] = formatter.format(i * interval);
}
hour.setDisplayedValues(hours);//This is what causes the error
hour.setWrapSelectorWheel(true);
hour.setMinValue(0);
hour.setMaxValue(hours.length - 1);
minute.setDisplayedValues(mins);//This is what causes the error
minute.setWrapSelectorWheel(true);
minute.setMinValue(0);
minute.setMaxValue(mins.length - 1);
}
}
}
This is the Error im getting
06-24 15:15:50.509 2006-2006/com.E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.mye.appo, PID: 2006
java.lang.IndexOutOfBoundsException: setSpan (2 ... 2) ends beyond length 0
at android.text.SpannableStringBuilder.checkRange(SpannableStringBuilder.java:1018)
at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:611)
at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:607)
at android.text.Selection.setSelection(Selection.java:76)
at android.widget.EditText.setSelection(EditText.java:92)
at android.widget.NumberPicker$SetSelectionCommand.run(NumberPicker.java:2189)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
I found that i can prevent this crash if set the displayed values from the fragment or activity only when if(this.isVisible())
from what i found this seems to be a life cycle issue
回答1:
due to bug - try to set before u use NumberPicker object:
NumberPicker.setSaveFromParentEnabled(false);
NumberPicker.setSaveEnabled(false);
i opened a issue of this & other bug of number picker:
reported picker bug at code.google
来源:https://stackoverflow.com/questions/31030704/android-numberpicker-index-outofbounds-rotation