android numberpicker索引越界旋转 [英] android numberpicker index outofbounds rotation

查看:121
本文介绍了android numberpicker索引越界旋转的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建自定义时间选择器.但是遇到的问题是,当我旋转屏幕时.同样,如果我按下主屏幕按钮离开应用程序,当我再次遇到相同错误时,它也会崩溃.

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

这是我收到的错误

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())时才设置片段或活动中的显示值,才能防止此崩溃

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

推荐答案

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

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:

在code.google上报告了选择器错误

这篇关于android numberpicker索引越界旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆