B4J Question [SOLVED] B4XImageView not initialized

techknight

Well-Known Member
Licensed User
Longtime User
I am attempting to add and initialize a B4X ImageView in code, as such:

B4X:
'Renders a resized image
Private Sub Draw_imageresized(ArgumentMap As Map)

    Dim Property As ControlProperties = ArgumentMap.Get("Property")
    Dim Graphic As GraphicObject = ArgumentMap.Get("GraphicObject")
        
    'define and initialize the image view
    Dim Image1 As B4XImageView
    Image1.Initialize(Me,"")
    
    'Determine if we are going to resize to the full screen
    If Property.DisplayName.ToLowerCase = "fullscreen" Then 
        Image1.mBase.Top = Settings.Top
        Image1.mBase.Left = Settings.Left
        Image1.mBase.Width = Settings.Width
        Image1.mBase.Height = Settings.Height
    Else
        Image1.mBase.Top = Property.Top*Settings.ScaleY
        Image1.mBase.Left = Property.Left*Settings.ScaleX
        Image1.mBase.Width = Property.Width*Settings.ScaleX
        Image1.mBase.Height = Property.Height*Settings.ScaleY
    End If
    
    'Store the image into a file from the Object ID of the current graphic object, then import and rescale. 
    Image1.Bitmap = fx.LoadImageSample(File.DirTemp,CallSub3(Callback, "ReturnImageAsFile", Graphic.ObjectID, Graphics), Image1.mBase.Width, Image1.mBase.Height)
    
    'Attach the image to the main parent view
    ScorePanel.AddNode(Image1.mBase, Image1.mBase.Left, Image1.mBase.Top, Image1.mBase.Width, Image1.mBase.Height)
End Sub

However, its crashing at: Image1.mBase.Top = Settings.Top

With this error: Caused by: java.lang.RuntimeException: Object should first be initialized (B4XView).

Which basically indicates mBase, or etc isnt initialized. But, I did initialize it at the top in code.

Thoughts?
 

techknight

Well-Known Member
Licensed User
Longtime User
Just to further clarify, I am doing this in code and not using an actual layout/designer, since this program is supposed to dynamically generate views on a window from properties stored in a database. Works on android, porting it over to B4J and running into the above.

I did try Dim Image1 As B4XImageView = XUIViewsUtils.CreateB4XImageView with no change.
 
Upvote 0

Alexander Stolte

Expert
Licensed User
Longtime User
I am doing this in code and not using an actual layout/designer
Thats why it is not working.

To initialize a custom view via code, you must do the following:
B4X:
    Dim xiv As B4XImageView
    xiv.Initialize(Me,"xiv")
   
    Dim xpnl_ImageBasePanel As B4XView = xui.CreatePanel("")
    xpnl_ImageBasePanel.SetLayoutAnimated(0,0,0,40dip,40dip)
   
    Dim xlbl_ImageLabel As Label
    xlbl_ImageLabel.Initialize("")
   
    Dim PropertyMap As Map
    PropertyMap.Initialize
    PropertyMap.Put("ResizeMode","FIT")
    PropertyMap.Put("Round",False)
    PropertyMap.Put("CornersRadius",0)
    PropertyMap.Put("BackgroundColor",0xFFAAAAAA)
   
    xiv.DesignerCreateView(xpnl_ImageBasePanel,xlbl_ImageLabel,PropertyMap)

It is therefore a mistake to create views by code. If something changes in the properties now, then you have to open the b4xlib every time and see which properties have been added and add them to the map.
 
Upvote 0

techknight

Well-Known Member
Licensed User
Longtime User
I was trying to work on that, I found example code that uses SetLayoutAnimated but that doesn't work on b4J

Loading a layout is ok, but i need to figure out how to resize the control on that layout based on given parameters upon initialization
 
Upvote 0

Sagenut

Expert
Licensed User
Longtime User
In the example after adding the layout it shown that it's possible to modify the mbase properties.
But maybe it depends even how the custom view is made.
 
Upvote 0

Alexander Stolte

Expert
Licensed User
Longtime User
Loading a layout is ok, but i need to figure out how to resize the control on that layout based on given parameters upon initialization
Custom views, if they have also been developed responsively, resize in the Base_Resize event. It is rare that these are public, so they must be called up in this way:
B4X:
CallSub3(xiv,"Base_Resize",20dip,20dip)
However, there are also views such as the B4XImageView that have an "Update" property which then internally call the Base_Resize event, then you only have to call this:
B4X:
xiv.Update

Full Example:
B4X:
    xiv.mBase.Width = 20dip
    xiv.mBase.Height = 20dip
    'CallSub3(xiv,"Base_Resize",20dip,20dip)
    xiv.Update

It depends on how the custom view was developed.
 
Upvote 0

PaulMeuris

Active Member
Licensed User
Have you tried this?
B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
End Sub
Public Sub Initialize
    B4XPages.GetManager.LogEvents = True
End Sub
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    Dim ivx As B4XImageView = XUIViewsUtils.CreateB4XImageView
    Root.AddView(ivx.mBase,10,10,300,300)
    ivx.Load(File.DirAssets,"beatiful_place_05.jpg")
    ivx.ResizeMode = "FILL"
End Sub
1719323012797.png

1719323162568.png
 
Upvote 0

techknight

Well-Known Member
Licensed User
Longtime User
A version of that I have, I havent tested it yet. I am getting roadblocked at the ASView customview library now. (Different issue from this thread)
 
Upvote 0

techknight

Well-Known Member
Licensed User
Longtime User
Thats why it is not working.

To initialize a custom view via code, you must do the following:
B4X:
    Dim xiv As B4XImageView
    xiv.Initialize(Me,"xiv")
  
    Dim xpnl_ImageBasePanel As B4XView = xui.CreatePanel("")
    xpnl_ImageBasePanel.SetLayoutAnimated(0,0,0,40dip,40dip)
  
    Dim xlbl_ImageLabel As Label
    xlbl_ImageLabel.Initialize("")
  
    Dim PropertyMap As Map
    PropertyMap.Initialize
    PropertyMap.Put("ResizeMode","FIT")
    PropertyMap.Put("Round",False)
    PropertyMap.Put("CornersRadius",0)
    PropertyMap.Put("BackgroundColor",0xFFAAAAAA)
  
    xiv.DesignerCreateView(xpnl_ImageBasePanel,xlbl_ImageLabel,PropertyMap)

It is therefore a mistake to create views by code. If something changes in the properties now, then you have to open the b4xlib every time and see which properties have been added and add them to the map.

This ultimately worked, although I didnt need to do anything with the property maps or other special things. Closing for now as solved.
 
Upvote 0
Top