Android spinning device while viewing dialog preferences using TimePicker or NumberPicker crashes the application

I created a custom DialogPreference that uses two NumberPicker on it to allow the user to select the hour of the day, as well as another custom DialogPreference that allows the user to select the time of day using the TimePicker widget.

As long as the dialogue is not open, and I rotate the screen, everything works as desired. However, if the dialog is open and the screen is rotated, the entire application will crash. I placed breakpoints in the onSaveInstanceState and onRestoreInstanceState and confirmed that everything that is needed to restore DialogPreference is ordered, the onDialogClosed method even hits every time along with all the other custom code that I have custom DialogPreference .

I create the following:

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

I run Android 4.4.3 (KitKat) on a Verizon Galaxy Nexus when this happens.

The following is the value of stacktrace:

 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) 
+2
source share
1 answer

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

in the onCreate method, put the following after findviewbyId for the timer:

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

This eliminated the error. However, the dialogue does not remain open. But this is apparently the default functionality for all non-standard settings, so I'm happy with that.

Here are the docs for the two methods described above:

And here is the gist that I posted in my last working user settings:

+9
source

Source: https://habr.com/ru/post/978238/


All Articles