B4J Question B4J pages, trying to add 100 pictures

Shay

Well-Known Member
Licensed User
Longtime User
Hi

While I am running the below (Trying to load pictures 10 x 10) it crashes on different errors (Tried few things)
Based on also the following example I wrote:
https://www.b4x.com/android/forum/threads/b4x-bitmapcreator-layout-implode-animation.92141/#content

class global: Private img(101) As B4XView

B4X:
Sub ArrangeSquares()
    
Dim calcX, calcY As Int
Dim Width, Height As Int
Dim Counter As Int = 1
calcX = 0dip
calcY = 0dip
Width = (1280 / 2 / 160)*16dip
Height = (800 / 2 / 160)*16dip

For i=1 To 10
    For x=1 To 10
        Dim iv As ImageView
        iv.Initialize("img" & i & x)
        Dim img(Counter) As B4XView = iv
        Log(Counter & "," & i & "," & x)
        img(Counter) = xui.LoadBitmap(File.DirAssets,"row-" & i & "-col-" & x & ".png")
        Root.AddView(img(Counter), calcY, calcX, Width, Height)
        img(Counter).Tag = i & x
        calcY = calcY + Width

        If Counter = 100 Then
            Exit
        Else
            Counter = Counter + 1
        End If
    Next
    calcY = 0dip
    calcX = calcX + Height
Next

End Sub
 

Shay

Well-Known Member
Licensed User
Longtime User
If using the above code, getting this error during compile:
B4X:
Dim img(Counter) As B4XView = iv
javac 1.8.0_131
src\b4j\example\b4xpage4.java:114: error: incompatible types: ImageView cannot be converted to B4XViewWrapper[]
_img = (anywheresoftware.b4a.objects.B4XViewWrapper[])(_iv.getObject());

if removing:
Dim img(Counter) As B4XView = iv
crashing with this error: (on the addview command)

B4X:
Program started.
Error occurred on line: 40 (B4XPage4)
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at anywheresoftware.b4a.keywords.Common.CallSubDebug2(Common.java:460)
    at b4j.example.b4xpagesmanager._createpageifneeded(b4xpagesmanager.java:823)
    at b4j.example.b4xpagesmanager._showpage(b4xpagesmanager.java:321)
    at b4j.example.b4xpages._showpage(b4xpages.java:93)
    at b4j.example.b4xpage2._btndraw_click(b4xpage2.java:125)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:632)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:237)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:167)
    at sun.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:91)
    at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:98)
    at anywheresoftware.b4a.BA$1.run(BA.java:216)
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
    at anywheresoftware.b4a.debug.Debug.CallSub4(Debug.java:134)
    at anywheresoftware.b4a.debug.Debug.CallSubNew2(Debug.java:81)
    ... 29 more
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at anywheresoftware.b4a.debug.Debug.CallSub4(Debug.java:115)
    ... 30 more
Caused by: java.lang.ClassCastException: javafx.scene.image.Image cannot be cast to javafx.scene.Node
    at b4j.example.b4xpage4._arrangesquares(b4xpage4.java:120)
    at b4j.example.b4xpage4._b4xpage_created(b4xpage4.java:167)
    ... 35 more

if not using array of images, and adding only 1 picture, it works fine
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
img(Counter) = xui.LoadBitmap(File.DirAssets,"row-" & i & "-col-" & x & ".png")
Should this not be
B4X:
img(Counter).SetBitmap(xui.LoadBitmap(File.DirAssets,"row-" & i & "-col-" & x & ".png"))
?
 
Upvote 0

Shay

Well-Known Member
Licensed User
Longtime User
Thanks, it is working, here is the code:
B4X:
Dim iv As ImageView
iv.Initialize("img" & i & x)
img(Counter)= iv
Log(Counter & "," & i & "," & x)
mParent.AddView(img(Counter), calcY, calcX, Width, Height)
img(Counter).SetBitmap(xui.LoadBitmap(File.DirAssets,"row-" & i & "-col-" & x & ".png"))
 
Upvote 0
Top