Android Question [B4XPages] Class instance was not initialized (b4ximageview)

chompina

Member
Licensed User
Longtime User
i have trouble, have a page with 3 imgx(B4xImageView added in designer) , 'x' wich mean 1,2,3.

in page1 i have this piece of code that open next page (pageCargaEvidencia)
B4X:
Private Sub cmdSiguiente_Click
    Try
        pageCargaEvidencia.Initialize
        B4XPages.ShowPage("pageCargaEvidencia")
    Catch
        Log(LastException)
        pageCargaEvidencia.Initialize
        B4XPages.AddPageAndCreate("pageCargaEvidencia",pageCargaEvidencia)
        B4XPages.ShowPage("pageCargaEvidencia")
    End Try
End Sub


the page pageCargaEvidencia load correctly, here is img1,img2,img3 added in designer.
that page(pageCargaEvidencia) take a picture and put in img1, img2,img3 recursively.
I have 3 boolean to determine if img1 has been load a bmp, and img2 and img3, all correctly taken pictures 1, 2 and 3 without close the page..

imagine that i take only 1 picture.., img1 is loaded, img2 empty and img3 empty.

the problem is when i go back, with back key or B4xPages.ClosePage(me) and then..
click on cmdSiguiente_Click to show pageCargaEvidencia again, the B4XImageview (img1,img2,im3) is
still showing the picture that i take (in this case img1 is loaded, and img2 and img3 still empty)...

The trouble start Here if i take the picture again to load in the next B4XImageView (img2 in this case) , this occur:
-The boolean that i control the taked picture,, is reset to false, and the code try to load img1, not img2.
-The img1 try to load the picture taked, give me a error, B4XPages Class instance was not initialized (b4ximageview).

The code after take a picture with success true, i call PoneFoto(picdatafromcamera)
B4X:
Public Sub PoneFoto (bmp As B4XBitmap)

    If Tiene1=False Then       
        img1.ResizeMode="FILL"
        img1.Bitmap=bmp
        Tiene1=True
        Return
    End If

    If Tiene2=False Then
        img2.ResizeMode="FILL"
        img2.Bitmap=bmp
        Tiene2=True
        Return
    End If

    If Tiene3=False Then
        img3.ResizeMode="FILL"
        img3.Bitmap=bmp
        Tiene3=True
        Return
    End If

End Sub

Log
B4X:
** Activity (main) Resume **
Error occurred on line: 71 (B4XImageView)
java.lang.RuntimeException: Class instance was not initialized (b4ximageview)
    at anywheresoftware.b4a.debug.Debug.shouldDelegate(Debug.java:242)
    at b4a.example.b4ximageview._setresizemode(b4ximageview.java:74)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:351)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:146)
    at anywheresoftware.b4a.debug.Debug.delegate(Debug.java:262)
    at b4a.example.p_cargaevidencia._ponefoto(p_cargaevidencia.java:352)
    at b4a.example.p_cargaevidencia$ResumableSub_TomaFoto.resume(p_cargaevidencia.java:333)
    at anywheresoftware.b4a.shell.DebugResumableSub$DelegatableResumableSub.resumeAsUserSub(DebugResumableSub.java:48)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:351)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:146)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:193)
    at anywheresoftware.b4a.shell.DebugResumableSub$DelegatableResumableSub.resume(DebugResumableSub.java:43)
    at anywheresoftware.b4a.BA.checkAndRunWaitForEvent(BA.java:267)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:139)
    at anywheresoftware.b4a.BA$2.run(BA.java:387)
    at android.os.Handler.handleCallback(Handler.java:751)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:163)
    at android.app.ActivityThread.main(ActivityThread.java:6228)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
this error ocurr when go back a page, load again the closed page to take picture and the code execute again the line:
B4X:
img1.ResizeMode="FILL"

-why is not initialized?, because is added with designer and not need to be initialize.
-when i control the initialize to try solution of img1 with XUIViewsUtils.CreateB4XImageView, then the img1 is go empty.

the idea is when i close a page contains img1,img2 and img3, hold the data when i open it again, but
when i assign a data again to image (img1, or a empty img2..) give me the error that no initialized b4ximageview that assumed is initialized..
Why This?
 

chompina

Member
Licensed User
Longtime User
Such code, with the try / catch block, is never needed and should be removed.
Add the pages once when the program starts.
i did it.., when go back closepage(me) and then, showpage, ,, take picture, when assign the data raise error object no initialized, that i explain above.

i do a workaround to resolve it.
 
Upvote 0

chompina

Member
Licensed User
Longtime User
in B4XMainPage
B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    
    Public img1 As B4XImageView
    Public img2 As B4XImageView
    Public img3 As B4XImageView
    Public Tiene1 As Boolean
    Public Tiene2 As Boolean
    Public Tiene3 As Boolean
End Sub

in the pageCargaEvidencia
B4X:
Sub Class_Globals
    Private Root As B4XView 'ignore
    Private xui As XUI 'ignore
    
    Private bmptmp As Bitmap
    Private imgFoto2 As B4XImageView
    Private img3 As B4XImageView 'added with designer
    Private img2 As B4XImageView 'added with designer
    Private img1 As B4XImageView 'added with designer
End Sub

Public Sub Initialize As Object
    imgFoto2=XUIViewsUtils.CreateB4XImageView
    
    bmptmp.InitializeMutable(640dip,480dip)
    imgFoto2.Bitmap=bmptmp

    Return Me
End Sub
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    'load the layout to Root
    Root.LoadLayout("frmCargaEvidencia")
    
    img1.Bitmap=bmptmp
    img2.Bitmap=bmptmp
    img3.Bitmap=bmptmp
    
    B4XPages.MainPage.img1=img1
    B4XPages.MainPage.img2=img2
    B4XPages.MainPage.img3=img3
End Sub

When i take the picture with success i ran this sub PoneFoto (dataReturnedFromCamera) in the pageCargaEvidencia
B4X:
Public Sub PoneFoto (bmp As B4XBitmap)
    AsignaImgs
    
    If B4XPages.MainPage.Tiene1=False Then
        'img1=B4XPages.MainPage.img1
        img1.ResizeMode="FILL"
        img1.Bitmap=bmp
        B4XPages.MainPage.Tiene1=True
        Return
    End If
    
    If B4XPages.MainPage.Tiene2=False Then
        'img2=B4XPages.MainPage.img2
        img2.ResizeMode="FILL"
        img2.Bitmap=bmp
        B4XPages.MainPage.Tiene2=True
        Return
    End If
    
    If B4XPages.MainPage.Tiene3=False Then
        'img3=B4XPages.MainPage.img3
        img3.ResizeMode="FILL"
        img3.Bitmap=bmp
        B4XPages.MainPage.Tiene3=True
        Return
    End If
    
End Sub

private Sub AsignaImgs
    img1=B4XPages.MainPage.img1
    img2=B4XPages.MainPage.img2
    img3=B4XPages.MainPage.img3
End Sub

in resume, i declare same objects that i use in pageCargaEvidencia in the MainPage and assign the values.
when go back with back key or button... to load the page, i ran then initialize and showPage, i remove all code that you say
and addpage once in create like this
B4X:
'this is a pageLocation, not a pageCargaEvidencia'
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    'load the layout to Root
    Root.LoadLayout("frmLocation")
    B4XPages.AddPage("pageCargaEvidencia",pageCargaEvidencia)
End Sub

'button to open pageCargaEvidencia'
Private Sub cmdSiguiente_Click
'    Try
        pageCargaEvidencia.Initialize
'        B4XPages.ShowPage("pageCargaEvidencia")
'    Catch
'        Log(LastException)
'        pageCargaEvidencia.Initialize
'        B4XPages.AddPageAndCreate("pageCargaEvidencia",pageCargaEvidencia)
'        B4XPages.ShowPage("pageCargaEvidencia")
'    End Try
    B4XPages.ShowPage("pageCargaEvidencia")
End Sub

with this workaround i resolve it the problem b4ximageview not initialized, i have labels i not write here to clean de code, but same error of b4xview not initialized. i solve it too with the same idea.

another not complicated solution?
thanks in advance for your help
 
Upvote 0

chompina

Member
Licensed User
Longtime User
hi, i´m very busy last week.

upload 2 project example in 1 zip file.
A project, with workaround solution that i expected the behavior worked.

B project, that supposed to be logical work code.
B project, you can take 3 photo with small preview, delete preview and take photo again, the issue is when you go back page and go to photo page, when take photo or try to delete preview, raise a initialize error.
 

Attachments

  • A and B small Project.zip
    96.7 KB · Views: 198
Upvote 0

chompina

Member
Licensed User
Longtime User
hi, i downloaded and compiled with no error.

Compiling process:
B4A Versión: 11.80
Parseando código.    (0.33s)
    Java Versión: 8
Building folders structure.    (0.07s)
Corriendo accion personalizada.    (0.20s)
Compilando código.    (0.42s)
Compilado códigos de diseños.    (0.07s)
Organizando librerías.    (0.00s)
    (AndroidX SDK)
Compilando los recursos    (0.44s)
Enlazando los recursos    (1.31s)
Compilando el código Java generado.    (0.06s)
Buscando librerias que necesitan ser dexed.    (0.02s)
Dex code    (2.03s)
Dex merge    (5.56s)
Copiando los recursos de librerias    (0.78s)
ZipAlign file.    (0.18s)
Firmando fichero de paquete (clave de depuración)    (1.99s)
Instalando archivo al dispositivo.    (28.86s)
    Device serial: XXXXXXXXXX
Completado con éxito

selected library


path
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
B4X:
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    B4XPages.AddPage("pageCargaEvidencia",pageCargaEvidencia)
End Sub

Private Sub cmdIngresar_Click
    pageCargaEvidencia.Initialize
    B4XPages.ShowPage("pageCargaEvidencia")
End Sub
This code is wrong.

Correct code:
B4X:
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    pageCargaEvidencia.Initialize
    B4XPages.AddPage("pageCargaEvidencia",pageCargaEvidencia)
End Sub

Private Sub cmdIngresar_Click
    B4XPages.ShowPage("pageCargaEvidencia")
End Sub
 
Upvote 1
Cookies are required to use this site. You must accept them to continue using the site. Learn more…