Hi,
I am trying to load a layout I created in the designer using a class and trying to set values to object in that layout but having issues in doing it.
My Main Activity looks like this:
Main:
The Class looks like this:
MyClass:
My Layout file is called 'Demo' and that just has one label called 'label1'.
When I try to change the text after loading the layout it says 'Object should first be initialized (Label).'
However, I then tried to load the layout without using the class, and loaded it using the following code and it works fine:
Main:
Do I need to initialize the label even knowing I am calling it from a layout I created in the designer when I call it from a class ?
In my example I am trying to load the layout from a class and then change the text of the label from my activity.
I am trying to load a layout I created in the designer using a class and trying to set values to object in that layout but having issues in doing it.
My Main Activity looks like this:
Main:
B4X:
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
End Sub
Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.
Dim MyClassDemo As MyClass
Private Label1 As Label
End Sub
Sub Activity_Create(FirstTime As Boolean)
'Do not forget to load the layout file created with the visual designer. For example:
'Activity.LoadLayout("Layout1")
MyClassDemo.Initialize(Activity)
Label1.text = "hello this is a test"
End Sub
Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
The Class looks like this:
MyClass:
B4X:
'Class module
Sub Class_Globals
Dim MyActivity As Activity
End Sub
'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize(act As Activity)
MyActivity = act
MyActivity.LoadLayout("demo")
End Sub
My Layout file is called 'Demo' and that just has one label called 'label1'.
When I try to change the text after loading the layout it says 'Object should first be initialized (Label).'
B4X:
PackageAdded: package:b4a.example.classdemo
** Activity (main) Create, isFirst = true **
java.lang.RuntimeException: Object should first be initialized (Label).
at anywheresoftware.b4a.AbsObjectWrapper.getObject(AbsObjectWrapper.java:46)
at anywheresoftware.b4a.objects.TextViewWrapper.setText(TextViewWrapper.java:39)
at b4a.example.classdemo.main._activity_create(main.java:303)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:174)
at b4a.example.classdemo.main.afterFirstLayout(main.java:98)
at b4a.example.classdemo.main.access$100(main.java:16)
at b4a.example.classdemo.main$WaitForLayout.run(main.java:76)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:213)
at android.app.ActivityThread.main(ActivityThread.java:5225)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:741)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
at dalvik.system.NativeStart.main(Native Method)
** Activity (main) Resume **
However, I then tried to load the layout without using the class, and loaded it using the following code and it works fine:
Main:
B4X:
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
End Sub
Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.
Private Label1 As Label
End Sub
Sub Activity_Create(FirstTime As Boolean)
'Do not forget to load the layout file created with the visual designer. For example:
Activity.LoadLayout("Demo")
Label1.text = "hello this is a test"
End Sub
Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
Do I need to initialize the label even knowing I am calling it from a layout I created in the designer when I call it from a class ?
In my example I am trying to load the layout from a class and then change the text of the label from my activity.