gpt4 book ai didi

android numberpicker 索引越界旋转

转载 作者:太空狗 更新时间:2023-10-29 13:19:22 26 4
gpt4 key购买 nike

我正在尝试创建自定义时间选择器。但是遇到的问题是当我旋转屏幕时。此外,如果我按下主页按钮离开应用程序,当我返回时出现相同的错误时它会崩溃。

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);
}
}
}

这是我得到的错误

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)

我发现如果仅在 if(this.isVisible()) 时设置 fragment 或 Activity 的显示值,我可以防止此崩溃

从我发现这似乎是一个生命周期问题

最佳答案

由于错误 - 在您使用 NumberPicker 对象之前尝试设置:

NumberPicker.setSaveFromParentEnabled(false);       
NumberPicker.setSaveEnabled(false);

我打开了一个关于这个和其他数字选择器错误的问题:

reported picker bug at code.google

关于android numberpicker 索引越界旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31030704/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com