Android Question Array of Bitmaps is not working

Martin Domian

Member
Licensed User
Longtime User
I wanted to create an array of images because they'll be needed in a later loading routine and will be loaded using an index corresponding to the number in the array.

But it keeps crashing when INIT the bitmap.
BM(i).Initialize(File.DirAssets, Filename)

If I use Dim BM1 as ... BM2 as ... it works, but that's nonsense.
Does anyone know what's wrong here?

LoadImages:
    Dim BM(1 To 11) As Bitmap
    Dim Filename As String
    For i = 1 To 11     
        Filename = "CL" & NumberFormat(i, 2, 0) & ".png"
        If File.Exists(File.DirAssets, Filename) Then
          BM(i).Initialize(File.DirAssets, Filename)
        Else
            Log ("Mist")
        End If       
    Next


Error:
java.lang.ArrayIndexOutOfBoundsException: length=1; index=1
 

stevel05

Expert
Licensed User
Longtime User
The syntax of your dim statement is incorrect. It should be
B4X:
Dim BM(10) As Bitmap
. The array index is zero based, so your loop should be
B4X:
For I = 0 to BM.Length -1
 
Upvote 1

Sagenut

Expert
Licensed User
Longtime User
@stevel05
But it should be
B4X:
Dim BM(11) As Bitmap
If not it will count 0 to 9.
 
Upvote 0
Top