DatePicker crash in samsung with android 5.0

2019-02-04 10:20发布

问题:

I am having trouble with creating a datepicker in samsung note 2 device with lollipop 5.0. The exception is:

java.util.IllegalFormatConversionException: %d can not format java.lang.String arguments
    at java.util.Formatter.badArgumentType(Formatter.java:1489)
    at java.util.Formatter.transformFromInteger(Formatter.java:1689)
    at java.util.Formatter.transform(Formatter.java:1461)
    at java.util.Formatter.doFormat(Formatter.java:1081)
    at java.util.Formatter.format(Formatter.java:1042)
    at java.util.Formatter.format(Formatter.java:1011)
    at java.lang.String.format(String.java:1803)
    at android.content.res.Resources.getString(Resources.java:1457)
    at android.content.Context.getString(Context.java:397)
    at android.widget.SimpleMonthView$MonthViewTouchHelper.getItemDescription(SimpleMonthView.java:684)
    at android.widget.SimpleMonthView$MonthViewTouchHelper.onPopulateNodeForVirtualView(SimpleMonthView.java:628)
    at com.android.internal.widget.ExploreByTouchHelper.createNodeForChild(ExploreByTouchHelper.java:377)
    at com.android.internal.widget.ExploreByTouchHelper.createNode(ExploreByTouchHelper.java:316)
    at com.android.internal.widget.ExploreByTouchHelper.access$100(ExploreByTouchHelper.java:50)
    at com.android.internal.widget.ExploreByTouchHelper$ExploreByTouchNodeProvider.createAccessibilityNodeInfo(ExploreByTouchHelper.java:711)
    at android.view.AccessibilityInteractionController$AccessibilityNodePrefetcher.prefetchDescendantsOfVirtualNode(AccessibilityInteractionController.java:1179)
    at android.view.AccessibilityInteractionController$AccessibilityNodePrefetcher.prefetchDescendantsOfRealNode(AccessibilityInteractionController.java:1091)
    at android.view.AccessibilityInteractionController$AccessibilityNodePrefetcher.prefetchDescendantsOfRealNode(AccessibilityInteractionController.java:1087)
    at android.view.AccessibilityInteractionController$AccessibilityNodePrefetcher.prefetchDescendantsOfRealNode(AccessibilityInteractionController.java:1087)
    at android.view.AccessibilityInteractionController$AccessibilityNodePrefetcher.prefetchDescendantsOfRealNode(AccessibilityInteractionController.java:1087)
    at android.view.AccessibilityInteractionController$AccessibilityNodePrefetcher.prefetchDescendantsOfRealNode(AccessibilityInteractionController.java:1087)
    at android.view.AccessibilityInteractionController$AccessibilityNodePrefetcher.prefetchDescendantsOfRealNode(AccessibilityInteractionController.java:1087)
    at android.view.AccessibilityInteractionController$AccessibilityNodePrefetcher.prefetchDescendantsOfRealNode(AccessibilityInteractionController.java:1087)
    at android.view.AccessibilityInteractionController$AccessibilityNodePrefetcher.prefetchDescendantsOfRealNode(AccessibilityInteractionController.java:1087)
    at android.view.AccessibilityInteractionController$AccessibilityNodePrefetcher.prefetchDescendantsOfRealNode(AccessibilityInteractionController.java:1087)
    at android.view.AccessibilityInteractionController$AccessibilityNodePrefetcher.prefetchDescendantsOfRealNode(AccessibilityInteractionController.java:1087)
    at android.view.AccessibilityInteractionController$AccessibilityNodePrefetcher.prefetchAccessibilityNodeInfos(AccessibilityInteractionController.java:888)
    at android.view.AccessibilityInteractionController.findAccessibilityNodeInfoByAccessibilityIdUiThread(AccessibilityInteractionController.java:155)
    at android.view.AccessibilityInteractionController.access$400(AccessibilityInteractionController.java:53)
    at android.view.AccessibilityInteractionController$PrivateHandler.handleMessage(AccessibilityInteractionController.java:1236)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:145)
    at android.app.ActivityThread.main(ActivityThread.java:5944)
    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:1389)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1184)

I have used the example provided in the official android studio page:

public static class DatePickerFragment extends DialogFragment
            implements DatePickerDialog.OnDateSetListener {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the current date as the default date in the picker
        final Calendar c = Calendar.getInstance();
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);
        int day = c.get(Calendar.DAY_OF_MONTH);
        // Create a new instance of DatePickerDialog and return it

        DatePickerDialog dpd = new DatePickerDialog(getActivity(), this,year, month, day);
        return dpd;


    }
    @Override
    public void onDateSet(DatePicker view, int year, int month, int day) {
        // get the date here

    }
}

I call it like this:

DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getFragmentManager(), "datePicker");

In other devices works fine.

I also tried creating my own layout with a DatePicker in it, really simple but also crashes. Is this some Android bug?

回答1:

As Ridcully says, its an issue with Samsung's 5.0 implementation

Workaround suggestions can be found here. Unfortunately none are the most pretty.

DatePicker crashes on my device when clicked (with personal app)



回答2:

Forget about the built-in date picker. There's only one way to get rid of the crash: use a library that provides an independent implementation.

For a date picker fragment: https://github.com/flavienlaurent/datetimepicker

For a date picker widget: https://github.com/SingleCycleKing/CustomTimePicker (this is more a starting point than a ready-to-use-solution)



回答3:

As alanv noticed, this is a bug of Samsung's Android 5 implementation.

I set up a different theme which shows the date picker with the (older) spinners instead of the calendar view. In the code, I check if the app is running on a samsung device with lollipop and a language other then english, and if so, I apply the theme with the spinners. This works well and there are no other libraries or other hacks necessary.

If needed I can add the theme and how to apply it to this answer.