53

Using a couple android.widget.NumberPicker in my Android App view and I get the following message when I hold down on one of the numbers arrows:-

FATAL EXCEPTION: main 
java.lang.NullPointerException at android.widget.NumberPicker$ChangeCurrentByOneFromLongPressCommand.run(NumberPicker.java:2245)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5419)

Anyone got any ideas on how I can stop this problem?

UPDATE: From the comments so far, it seems that this exception only happened on Samsung devices with Android 4.3.

Joe
  • 14,039
  • 2
  • 39
  • 49
user3145914
  • 547
  • 3
  • 4
  • 1
    How can we generate your codes from your error log? – Batuhan Coşkun Dec 30 '13 at 09:54
  • Because the error is coming from the library code, null pointer exception at android.widget.NumberPicker. it's not my code that causing the problem, and I'm looking for work around. – user3145914 Dec 31 '13 at 02:28
  • 4
    This might be a device-specific bug, so far I am seeing it in my logs only from Samsung devices with Android 4.3. Do you get it if you run your app in the official emulator? – Joe Jan 02 '14 at 19:02
  • Yeah - not in the emulator, and I'm testing on a Samsung on 4.3. So I can just not worry about it and the problem will go away by itself? – user3145914 Jan 09 '14 at 06:30
  • I had the exact same issue on real device: Galaxy S4 with Android 4.3 ... any more info? – neteinstein Jan 09 '14 at 13:51
  • I saw it also only on Galaxy S3 w/ Android 4.3 – Amir Uval Jan 16 '14 at 14:23
  • 4
    I have opened a new issue Android issue tracker https://code.google.com/p/android/issues/detail?id=65444 – laaptu Jan 31 '14 at 08:32
  • How could we raise this issue to samsung. Any idea? – Rino Feb 06 '14 at 05:26
  • An [answer to another post here](http://stackoverflow.com/a/18894529/1023092) suggest posting issues to [Samsung's developer forum](http://developer.samsung.com/forum/). Anyone want to try? – Joe Feb 19 '14 at 22:43
  • @user3145914 check: http://sampleprogramz.com/android/numberpicker.php – Khaled.K Mar 26 '14 at 06:26
  • Did you check exact version that you use on each device ? Usually version 4.3 is not sufficient and there could be another minor number and maybe build/revision number. Also Samsung takes android release and do his stuff into it, so it is possible that they removed that. Check if you have access to sources. Here for instance isn't inner class on yours line 2245 but 2130 https://github.com/android/platform_frameworks_base/blob/master/core/java/android/widget/NumberPicker.java – Lukino Mar 28 '14 at 12:27
  • Did you happen to find a way to fix/prevent the problem? I have the same issue with users with 4.3 samsungs – u2tall Oct 07 '14 at 03:55
  • Same here, on a Samsung Galaxy S3, also Android 4.3 – P Kuijpers Jul 29 '16 at 21:04

3 Answers3

1

So the Samsung NumberPicker is slightly different from AOSP. The code for ChangeCurrentByOneFromLongPressCommand class has some extra logic including

if (!mIsDeviceDefault) {
    // ...
} else if (mDecrementButton.isShown() || mIncrementButton.isShown()) {
    // ...
  • where mIsDeviceDefault is true when you're using one of the Theme.DeviceDefault themes (which is the default theme for apps starting Android 4.x)
  • where mDecrementButton and mIncrementButton are null if mHasSelectorWheel is true, which happens if the layout resource ID for NumberPicker specified by its style is one of
    • DEFAULT_LAYOUT_RESOURCE_ID / @layout/number_picker
    • TOUCHWIZ_DARK_LAYOUT_RESOURCE_ID / @layout/tw_number_picker_dark
    • TOUCHWIZ_LIGHT_LAYOUT_RESOURCE_ID / @layout/tw_number_picker_light

which suggests that this would happen all the time.

Here are all the possible styles:

<style name="Widget.NumberPicker">
    <item name="orientation">1</item>
    <item name="fadingEdge">2000</item>
    <item name="fadingEdgeLength">50dp</item>
    <item name="internalLayout">@layout/number_picker</item>
</style>

<style name="Widget.Holo.NumberPicker">
    <item name="solidColor">@color/transparent</item>
    <item name="internalLayout">@layout/number_picker_with_selector_wheel</item>
    <item name="selectionDivider">@drawable/numberpicker_selection_divider</item>
    <item name="selectionDividerHeight">2dp</item>
    <item name="selectionDividersDistance">48dp</item>
    <item name="internalMaxHeight">180dp</item>
    <item name="internalMinWidth">64dp</item>
    <item name="virtualButtonPressedDrawable">?attr/selectableItemBackground</item>
</style>

<style name="Widget.DeviceDefault.NumberPicker">
    <item name="internalLayout">@layout/tw_number_picker_dark</item>
</style>

<style name="Widget.DeviceDefault.Light.NumberPicker">
    <item name="internalLayout">@layout/tw_number_picker_light</item>
</style>

Source: Downloaded Android 4.3 firmware for Samsung Galaxy S3, extracted framework files using ext4 unpacker, decompiled framework.jar and framework2.jar using Universal Deodexer V5, explored the results including framework-res.apk using JADX.

How to fix

After your NumberPicker is constructed use reflection to check if ImageButton mDecrementButton and ImageButton mIncrementButton are null. If they are use reflection to set them each to a new ImageButton(numberPicker.getContext()).

Only apply this logic when running on one of the affected devices (check Build constants).

Eugen Pechanec
  • 37,669
  • 7
  • 103
  • 124
0

This how I am implementing NumberPicker in my application, and it is working well for me:

java code , in your onCreate() :

NumberPicker numberpicker = (NumberPicker) findViewById(R.id.np_from);
numberpicker.setMaxValue(100);
numberpicker.setMinValue(16);

XML code :

           <NumberPicker
            android:id="@+id/np_from"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="20dp"
            android:width="100dip" />

I hope it helps.

droidchef
  • 2,237
  • 1
  • 18
  • 34
ahmed_khan_89
  • 2,755
  • 26
  • 49
0

http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.3_r1/android/view/View.java#View.clearAccessibilityFocus%28%29

Looking at all 4.3 versions it's pretty clear the code is most likely calling clearAccessibilityFocus().

Assuming accurate line numbers I'd search in there for a likely cause. Nothing jumps out at me, in fact most of the accesses have good null checks.

ggb667
  • 1,881
  • 2
  • 20
  • 44