Android Question Object Should be Initialized (JavaObject)

hi guys, i read this thread

Fit or fill image

here's my code :
B4X:
Sub setImageView(iv As ImageView, bmp As Bitmap, scale As String)
    Dim jo As JavaObject
    jo.RunMethod("setImageBitmap", Array(bmp))
    Select scale
        Case "cover"
            jo.RunMethod("setScaleType", Array("CENTER_CROP"))
        Case "contain"
            jo.RunMethod("setScaleType", Array("CENTER_INSIDE"))
        Case "fill"
            jo.RunMethod("setScaleType", Array("FILL_XY"))
    End Select
End Sub

but i got this error :

B4X:
Logger connected to:  vivo vivo 1724
--------- beginning of system
--------- beginning of main
Copying updated assets files (11)
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = true **
** Activity (intoduction) Create, isFirst = true **
** Activity (intoduction) Resume **
** Activity (intoduction) Pause, UserClosed = true **
** Activity (home) Create, isFirst = true **
Error occurred on line: 46 (Home)
java.lang.RuntimeException: Object should first be initialized (JavaObject).
    at anywheresoftware.b4a.AbsObjectWrapper.getObject(AbsObjectWrapper.java:50)
    at anywheresoftware.b4j.object.JavaObject.getCurrentClass(JavaObject.java:258)
    at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:118)
    at b4a.example.home._setimageview(home.java:406)
    at b4a.example.home._activity_create(home.java:366)
    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:144)
    at b4a.example.home.afterFirstLayout(home.java:104)
    at b4a.example.home.access$000(home.java:17)
    at b4a.example.home$WaitForLayout.run(home.java:82)
    at android.os.Handler.handleCallback(Handler.java:790)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:192)
    at android.app.ActivityThread.main(ActivityThread.java:6702)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:549)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:826)
** Activity (home) Resume **

how it should be?
 

Computersmith64

Well-Known Member
Licensed User
Longtime User
When you read the thread you linked to, you missed something:
B4X:
Public Sub SetImageView(iv As ImageView,bmp As Bitmap, scale As String)
    Dim jo As JavaObject=iv
...
Note in the 2nd line of the code above, the JavaObject is being initialized by assigning the ImageView to it (Dim jo As JavaObject = iv).

- Colin.
 
Upvote 0
Top