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

Filippo

Expert
Licensed User
Longtime User
Hi,

I have this error in Crashlytics dashboard:
Fatal Exception: java.lang.ArithmeticException: divide by zero
at anywheresoftware.b4a.objects.drawable.CanvasWrapper$BitmapWrapper.initializeSampleImpl(CanvasWrapper.java:606)
at anywheresoftware.b4a.objects.drawable.CanvasWrapper$BitmapWrapper.InitializeSample(CanvasWrapper.java:598)
at anywheresoftware.b4a.keywords.Common.LoadBitmapSample(Common.java:1389)
at fg.speedpilot_lite.main._vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv5(main.java:2340)
at fg.speedpilot_lite.main._vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv2(main.java:2310)
at fg.speedpilot_lite.main._activity_resume(main.java:860)
at java.lang.reflect.Method.invoke(Method.java)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:221)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:201)
at fg.speedpilot_lite.main.afterFirstLayout(main.java:111)
at fg.speedpilot_lite.main.access$000(main.java:17)
at fg.speedpilot_lite.main$WaitForLayout.run(main.java:83)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loopOnce(Looper.java:226)
at android.os.Looper.loop(Looper.java:313)
at android.app.ActivityThread.main(ActivityThread.java:8751)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:571)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1135)
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
You need to find out why the width or height are zero.

Another option is to set a limit for the size before you load the bitmap:
B4X:
btnReset.Width = Max(btnReset.Width, 20dip)
btnReset.Height = Max(btnReset.Height, 20dip)
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
Top