Android Question java.lang.IllegalArgumentException: what could be the cause in bitmap.crop, other than width/height < 0?

ema01

Member
Licensed User
Longtime User
Hello,
i'm having troubles interpreting this exception stack trace
B4X:
java.lang.IllegalArgumentException:
  at android.graphics.Bitmap.checkWidthHeight (Bitmap.java:428)
  at android.graphics.Bitmap.createBitmap (Bitmap.java:802)
  at anywheresoftware.b4a.objects.drawable.CanvasWrapper$BitmapWrapper.Crop (CanvasWrapper.java:587)
  at com.seletron.PedalBooster.main._appsettings_setlayout (main.java:998)
  at com.seletron.PedalBooster.main._pnlsettings_setlayout (main.java:3292)
  at com.seletron.PedalBooster.main$ResumableSub_Activity_Create.resume (main.java:779)
  at anywheresoftware.b4a.BA.checkAndRunWaitForEvent (BA.java:267)
  at anywheresoftware.b4a.BA.raiseEvent2 (BA.java:207)
  at anywheresoftware.b4a.BA.raiseEvent (BA.java:193)
  at anywheresoftware.b4a.keywords.Common$14.run (Common.java:1764)
  at android.os.Handler.handleCallback (Handler.java:873)
  at android.os.Handler.dispatchMessage (Handler.java:99)
  at android.os.Looper.loop (Looper.java:193)
  at android.app.ActivityThread.main (ActivityThread.java:6711)
  at java.lang.reflect.Method.invoke (Native Method)
  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:493)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:911)

The code that is *probably* causing the exception
B4X:
    Dim cvs As B4XCanvas
    Dim r As B4XRect
    Dim l As Float
    Dim t As Float
    Dim w As Float
    Dim h As Float

    [...]

    imgBgB.SetLayout(pnlBgSelector.Width/2 - l/2,lblBgChoose.Top + lblBgChoose.Height + vm,l,l) 'Dimensions should all be valid

    [...]

    w = imgBgB.Width*120.0/200.0
    l = (imgBgB.Width-w)/2
    t = (imgBgB.Width-w)/2
   
    cvs.Initialize(imgBgB)
    r.Initialize(l,t,l+w,t+w)
    cvs.ClearRect(cvs.TargetRect)
    cvs.DrawBitmap(LoadBitmap(File.DirAssets,"bgHexFlat.png").Crop(l,t,w,w),r) 'only place Bitmap.Crop is explicitely mentioned
    cvs.DrawBitmap(LoadBitmap(File.DirAssets,"imgNewBackgroundNotSelected.png"),cvs.TargetRect)
    cvs.Invalidate
    cvs.Release

The stack trace comes from the phone of a user that has been having issues since the last update of the app, altough this part of the code has been used for several releases for several months. Another app has had this code for years.

I don't think the problem is caused by width or height being < 0 for two reasons:
-In all cases i've encountered the exception explicitely mentioned width/height < 0
-There are checks in the sub that sets the layout, but i concede they may not be 100% bug proof

The device is a Cubot KingKong Mini 2 Rugged phone and while i would be inclined to blame the manufacturer (such phones never work okay in my experience) it's of course not a solution.
I tried generating a simillar device in the simulator (same resolution, memory, android version) and because i suspected something stupid was made by the mfg i also added a device that was forced to run as if landscape and portrait were "swapped" (appereance is portrait, but 100%x is higher than 100%y) but the application launched okay regardless

What can be the other reasons for an IllegalArgumentExcetpion?
 

agraham

Expert
Licensed User
Longtime User
It's difficult to see anything as you have posted the code without any context. It looks from the stack trace that the error occurs in a resumption of your Activity_Create which must include a Wait For or Sleep. It seems to call a Sub pnlsettings_setlayout which in turn calls a Sub appsettings_setlayout which calls BitmapWrapper.Crop.

This is just a guess, but Crop crops an existing Bitmap using the minimum of both the existing Bitmap width and height and the specified width and height. If the specified width and height are known to be non-zero my guess is that the existing Bitmap might have a zero width or height. If the Bitmap wasn't initialised I would expect a different Null error.
 
Upvote 0

ema01

Member
Licensed User
Longtime User
I will look into Crashlytics (was on the to-look list anyway)

@agraham: The interpretation is correct.
Activity_Create loads a splash screen which is put to sleep until _Create sets a flag, then control returns to load which loads the layout, then call pnlSettings_setLayout which in turn call appSettings_setLayout and there a bitmap is cropped. File exists as it's in the assets and so far only this specific uncommon phone has the problem
 
Upvote 0
Top