话框preference与TimePicker或NumberPi

话框preference与TimePicker或NumberPi

本文介绍了旋转Android设备,同时查看对话框preference与TimePicker或NumberPicker导致应用程序崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了一个自定义的对话preference ,它使用两个 NumberPicker 小部件就可以允许用户选择一天中的一小时,以及其他自定义对话preference ,允许用户通过选择一天中的时间 TimePicker 小工具。

只要对话框未打开,我旋转屏幕,一切正常所需。然而,如果对话是开放的,在屏幕旋转那么整个应用程序崩溃。我已经放在破发点中的的onSaveInstanceState onRestoreInstanceState 的方法和已验证了所需要的一切,恢复对话preference 是秩序, onDialogClosed 方法连撞每次与所有其他自定义code我有我的自定义对话preference

我在建设有以下内容:

  • 最低SDK:16
  • 目标SDK:20
  • 编译SDK:20
  • 在生成工具:20.0.0

我在Verizon的Galaxy Nexus的运行Android 4.4.3(奇巧)时,这种情况的发生。

下面是堆栈跟踪给定的:

  java.lang.IndexOutOfBoundsException:setSpan(4 ... 4)超出长度0结束
  在android.text.SpannableStringBuilder.checkRange(SpannableStringBuilder.java:1016)
  在android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:592)
  在android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:588)
  在android.text.Selection.setSelection(Selection.java:76)
  在android.widget.EditText.setSelection(EditText.java:87)
  在android.widget.NumberPicker $ SetSelectionCommand.run(NumberPicker.java:2123)
  在android.os.Handler.handleCallback(Handler.java:733)
  在android.os.Handler.dispatchMessage(Handler.java:95)
  在android.os.Looper.loop(Looper.java:136)
  在android.app.ActivityThread.main(ActivityThread.java:5001)
  在java.lang.reflect.Method.invokeNative(Method.java)
  在java.lang.reflect.Method.invoke(Method.java:515)
  在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:785)
  在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
  在dalvik.system.NativeStart.main(NativeStart.java)
 

解决方案

我找到了解决这个问题就在这里:<一href="https://$c$c.google.com/p/android/issues/detail?id=22754#c5">https://$c$c.google.com/p/android/issues/detail?id=22754#c5,它说以下内容:

  tp.setSaveFromParentEnabled(假);
tp.setSaveEnabled(真正的);
 

这摆脱了错误。但是,对话框不会继续开放。但是,这似乎是所有非定制preferences默认功能,所以我很高兴。

下面是上面使用的文档的两种方法:

  • <$c$c>setSaveFromParentEnabled
  • <$c$c>setSaveEnabled

这里是我已为我的最后工作自preferences学家:

I've made a custom DialogPreference which uses two NumberPicker widgets on it to allow a user to select an hour of the day as well as another custom DialogPreference which allows a user to select a time of day using a TimePicker widget.

As long as the dialog is not open and I rotate the screen, everything works as desired. However, if the dialog is open and the screen is rotated then the entire app crashes. I've placed break points in the onSaveInstanceState and onRestoreInstanceState methods and have verified that everything that is needed to restore the DialogPreference is in order, the onDialogClosed method is even hit everytime along with all other custom code that I have in my custom DialogPreference.

I'm building with the following:

  • Minimum SDK: 16
  • Target SDK: 20
  • Compile SDK: 20
  • Build Tools: 20.0.0

I'm running Android 4.4.3 (KitKat) on a Verizon Galaxy Nexus when this is happening.

Here is the stacktrace given:

java.lang.IndexOutOfBoundsException: setSpan (4 ... 4) ends beyond length 0
  at android.text.SpannableStringBuilder.checkRange(SpannableStringBuilder.java:1016)
  at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:592)
  at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:588)
  at android.text.Selection.setSelection(Selection.java:76)
  at android.widget.EditText.setSelection(EditText.java:87)
  at android.widget.NumberPicker$SetSelectionCommand.run(NumberPicker.java:2123)
  at android.os.Handler.handleCallback(Handler.java:733)
  at android.os.Handler.dispatchMessage(Handler.java:95)
  at android.os.Looper.loop(Looper.java:136)
  at android.app.ActivityThread.main(ActivityThread.java:5001)
  at java.lang.reflect.Method.invokeNative(Method.java)
  at java.lang.reflect.Method.invoke(Method.java:515)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
  at dalvik.system.NativeStart.main(NativeStart.java)
解决方案

I found the solution to this problem here: https://code.google.com/p/android/issues/detail?id=22754#c5, which says the following:

tp.setSaveFromParentEnabled(false);
tp.setSaveEnabled(true);

This got rid of the error. However, the dialog doesn't stay open. But this seems to be the default functionality for all non-custom preferences, so I'm happy with it.

Here are the docs for the two methods used above:

And here are gists that I've posted of my final working custom Preferences:

这篇关于旋转Android设备,同时查看对话框preference与TimePicker或NumberPicker导致应用程序崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 21:39