Android Question Library Error: Object should first be initialized (Activity)

stevew

Member
Licensed User
Longtime User
I have a class that calls an activity with CallSubDelayed. It works as intended. I compile it to a library and it runs, but the activity doesn't appear.

I searched for answers and found I should add to the Manifest:

AddApplicationText(<activity android:name="mypath,myactivity"/>).

With this I get a message in the log
** Activity (main) Pause, UserClosed = false **
** Activity (myactivity) Create, isFirst = true **
myactivity:Activity_Create (from my log entry when the Activity_Create sub is called )
java.lang.RuntimeException: Object should first be initialized (Activity).

I have a sense I'm missing something simple.

Thanks for any suggestions!
 

Yayou49

Active Member
Licensed User
Could you post your project as zip or, at least, part of involved code ?
 
Upvote 0

stevew

Member
Licensed User
Longtime User
I stripped down my app. The code is below.

I first run with Main, Starter(empty), actTestClass and TestClass in the project, without AddApplicationText(<activity android:name="b4a.testclass.TestClass"/>) in the manifest.

The activity shows up with the textbox loaded with the number; clicking the button closes the activity, and the Toast shows the number. Just as intended.

I compile the library, remove the testClass and actTestClass, restart the project, and load the Library.

When I run it with or without using the library, with or without AddApplicationText(<activity android:name="b4a.testclass.TestClass"/>) n the Manifest I get the error

actTestClass:Activity_Create
java.lang.RuntimeException: Object should first be initialized (Activity).


Thanks in advance!



This is Main

B4X:
#Region  Project Attributes
    #ApplicationLabel: B4A TestClass
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
   
    #LibraryName: TestClass
    #LibraryAuthor: Steve Wheaton
    #LibraryVersion:1.0
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

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 btnStart As Button
    Dim cTest As TestClass
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("main")
    cTest.Initialize(Me,cTest)
    cTest.ID = 12
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub


Sub btnStart_Click
    cTest.LoadItems
End Sub

Public Sub ClassResult(ID As Long)
    ToastMessageShow(ID, False)
End Sub


This is the Class TestClass

B4X:
'Class TestClass

'ver 1.0
'Sept 5/18



Sub Class_Globals
   
    Private mCallback, myobj As Object
    Private mID As Long
   
   
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize(CallBack As Object,MyObject As TestClass)
    Log("TestClass:Initialize")
   
    mCallback = CallBack
    myobj = MyObject
End Sub

Public Sub LoadItems
    Log("TestClass:LoadItems")
    CallSubDelayed2(actTestClass,"StartAct",myobj)
    Sleep(0)
End Sub

Public Sub Result(ID As Long)
    Log("TestClass:Result")
    mID = ID
    CallSubDelayed2(mCallback,"ClassResult",mID)
End Sub



Public Sub setID (val As Long)
    mID = val
End Sub

Public Sub getID As Long
    Return mID
End Sub

This is the Activity actTestClass
B4X:
#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
    #ExcludeFromLibrary: False
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Private mCallback As TestClass
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 btnQuit As Button
    Dim pnlDisplay As Panel
    Private mNumber As Long
    Private EditText1 As EditText
    Private butQuit As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Log("actTestClass:Activity_Create")
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("frmTestClass")
    pnlDisplay.Initialize("")
    pnlDisplay.Color =Colors.White
    pnlDisplay.Visible = True
   
   
    'Activity.AddView(pnlDisplay,0,40%y,100%x,60%y)
    Activity.AddView(pnlDisplay,20dip,400dip,50%x,50%y)
   
End Sub

Public Sub StartAct(Callback As TestClass)'
    Log("actTestClass:StartAct")
    mCallback = Callback
    LoadItems
End Sub


Sub LoadItems
    Log("actTestClass:LoadItems")
    'Activity.Title = "Add Item:  " & RoomName
    Dim b As Boolean
    mNumber = mCallback.ID
    EditText1.Text = mNumber
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub




Sub butQuit_Click
    Log("actTextClass:btnQuit")
    CallSub2(mCallback,"Result",mNumber)
    Activity.Finish
End Sub

This is the Manifest

B4X:
'This code will be applied to the manifest file during compilation.
'You do not need to modify it in most cases.
'See this link for for more information: https://www.b4x.com/forum/showthread.php?p=78136
AddManifestText(
<uses-sdk android:minSdkVersion="5" android:targetSdkVersion="26"/>
<supports-screens android:largeScreens="true"
    android:normalScreens="true"
    android:smallScreens="true"
    android:anyDensity="true"/>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
CreateResourceFromFile(Macro, Themes.DarkTheme)
'End of default text.
AddApplicationText(<activity android:name="b4a.testclass.TestClass"/>)
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
I don't know if the "flow of your project" is good enough; anyway, you could pass to the class initialization not only "Me" (callback) but also a second "callback", like:

Public Sub Initialize(CallBack As Object,MyObject As TestClass, SecondCallback As Object)

cTest.Initialize(Me,cTest, SecondActivity)
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
I don't know if the "flow of your project" is good enough; anyway, you could pass to the class initialization not only "Me" (callback) but also a second "callback", like:

Public Sub Initialize(CallBack As Object,MyObject As TestClass, SecondCallback As Object)

cTest.Initialize(Me,cTest, SecondActivity)

Do not pay attention to the content of the class and to the property, it is just a my attempt to create a class template.
 

Attachments

  • Library test.zip
    9.1 KB · Views: 124
Upvote 0

stevew

Member
Licensed User
Longtime User
Thanks for reply LucaMs,

I understand the concept you have suggested. However, how do you initialize an activity? It does not have an Initialize function like a standard Class.
 
Upvote 0

stevew

Member
Licensed User
Longtime User
Actual that is exactly what I want to do.

If this is not possible, and I can pass in a ui (Activity) as you suggest,this is a solution. But it isn't the 'black box' approach I want and prefer. The Activity would be template like a Class, and the object would be created at runtime. I have assumed that Activity was the same as a class, but maybe there not?
 
Upvote 0
Top