Android Question LoadBitmapSample and java.lang.ArithmeticException - divide by zero

Filippo

Expert
Licensed User
Longtime User
Hi,

I have this error in Crashlytics dashboard:
This line : vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv2(main.java:2310)
points to this procedure:
B4X:
Private Sub ChangeResetButton(IsReset As Boolean)
    Dim Enabled, Disabled As BitmapDrawable
    Dim sld As StateListDrawable

    'bitmap state
    If IsReset Then
        Enabled.Initialize(LoadBitmapSample(File.DirAssets, "btn_reset_enabled.png", btnReset.Width, btnReset.Height))
        Disabled.Initialize(LoadBitmapSample(File.DirAssets, "btn_reset_disabled.png", btnReset.Width, btnReset.Height))
    Else
        Enabled.Initialize(LoadBitmapSample(File.DirAssets, "btn_next-speed_enabled.png", btnReset.Width, btnReset.Height))
        Disabled.Initialize(LoadBitmapSample(File.DirAssets, "btn_next-speed_disabled.png", btnReset.Width, btnReset.Height))
    End If

    'initialize statelistdrawable
    sld.Initialize
    'add states
    sld.AddState(sld.State_Pressed, Disabled)
    sld.AddState(sld.State_Enabled, Enabled)
    sld.AddState(sld.State_Disabled, Disabled)
    
    'set background as state
    btnReset.Background = sld
End Sub

The error is triggered by these 3 devices: Galaxy S10( android 12), Redmi 7( android 10), Honor 8S(android 9)
The control "btnReset" is initialized with certainty, it is in the main layout.
How can I catch the error?
Should I check before each "LoadBitmapSample" if btnReset.Width and btnReset.Height is greater than zero?

Thanks for the help
Filippo
 

Filippo

Expert
Licensed User
Longtime User
I have now changed my procedure so I hope that the error messages will be less or disappear completely.

B4X:
Private Sub ChangeResetButton(IsReset As Boolean)
    Dim Enabled, Disabled As BitmapDrawable
    Dim sld As StateListDrawable
    Dim Width, Height As Int
   
    Width = Max(btnReset.Width, 20dip)
    Height =  Max(btnReset.Height, 20dip)

    'bitmap state
    If IsReset Then
        Enabled.Initialize(LoadBitmapSample(File.DirAssets, "btn_reset_enabled.png", Width, Height)
        Disabled.Initialize(LoadBitmapSample(File.DirAssets, "btn_reset_disabled.png", Width, Height)
    Else
        Enabled.Initialize(LoadBitmapSample(File.DirAssets, "btn_next-speed_enabled.png", Width, Height)
        Disabled.Initialize(LoadBitmapSample(File.DirAssets, "btn_next-speed_disabled.png", Width, Height)
    End If

    'initialize statelistdrawable
    sld.Initialize
    'add states
    sld.AddState(sld.State_Pressed, Disabled)
    sld.AddState(sld.State_Enabled, Enabled)
    sld.AddState(sld.State_Disabled, Disabled)
   
    'set background as state
    btnReset.Background = sld
End Sub
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…