25

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?

Sufian
  • 5,997
  • 14
  • 60
  • 111
  • 1
    It's a bug in Samsung's version of Lollipop that is not present in AOSP. You will need to contact Samsung support. – alanv Feb 05 '15 at 18:26
  • 1
    I though the proguard cfg solved it but it's still crashing. The problem was in the style Theme.AppCompat for Samsung Note3 only. My resolution was to change the style of DatePicker `DatePickerDialog dpd = new DatePickerDialog(getActivity(),R.style.MyDatePicker, this, year, month, day);` – Vilson Hoxhallari Feb 11 '15 at 14:55
  • Hi @vilson could you explain R.style.MyDatePicker a bit more? What did you put in there? Thanks! – Alex Black Feb 15 '15 at 21:08
  • 1
    @AlexBlack ` ` it was crashing because the activity had the style `Theme.AppCompat` – Vilson Hoxhallari Feb 16 '15 at 08:55
  • Hmm, when I use that my datepicker looks very different, like a broken version of an old style. I'm thinking i might try an IF statement and only use that style on crashing devices somehow? – Alex Black Feb 21 '15 at 22:28
  • Would using new DatePickerDialog(getAcitivity(), android.R.style.Theme_DeviceDefault, ...) work? – Alex Black Feb 21 '15 at 22:43
  • 1
    @AlexBlack tried it but its still crashing, it displays the DatePicker and then crashes – Vilson Hoxhallari Feb 25 '15 at 10:09
  • Thanks for trying. Did you find any to keep its appearance the same (esp on lollipop) and still avoid the crash – Alex Black Feb 25 '15 at 16:53
  • I posted a new possible approach for a solution, would be great to get feedback if it works: http://stackoverflow.com/a/34853067/348378 – Raanan Jan 18 '16 at 11:08

3 Answers3

3

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)

Community
  • 1
  • 1
kassim
  • 3,476
  • 2
  • 21
  • 24
1

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)

stefan222
  • 661
  • 13
  • 30
  • How can you be sure that third party library wouldn't crash? – 김준호 Jul 21 '15 at 01:45
  • I've tested both libraries on a device where the build-in date picker crashed. Plus I released my apps using the libs and suddenly the crash reports stopped. This is not an exact proof, but I didn't receive any crashes caused by the libraries itself. Well, there's never a guarantee that open source libs will be free of bugs ;-) – stefan222 Jul 21 '15 at 06:37
0

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.

Ridcully
  • 22,583
  • 7
  • 66
  • 81