Android Question APP crashes with error of not initialising the Label

Peter Lewis

Active Member
Licensed User
Longtime User
Hi

I have my Main Activity calling a sub called using

B4X:
CallSubDelayed(readbarc, "Read_Barcode_update")

Then when I run it the App crashes with the following errors

B4X:
** Activity (main) Pause, UserClosed = false **
** Activity (readbarc) Create, isFirst = false **
** Activity (readbarc) Resume **
** Activity (readbarc) Pause, UserClosed = false **
sending message to waiting queue (OnActivityResult)
** Activity (readbarc) Create, isFirst = false **
running waiting messages (1)
Error occurred on line: 46 (readbarc)
java.lang.RuntimeException: Object should first be initialized (Label).
    at anywheresoftware.b4a.AbsObjectWrapper.getObject(AbsObjectWrapper.java:50)
    at anywheresoftware.b4a.objects.TextViewWrapper.setText(TextViewWrapper.java:38)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:710)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:339)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:249)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:134)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:167)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:163)
    at com.AB.ABZxing.ABZxing$1.ResultArrived(ABZxing.java:96)
    at anywheresoftware.b4a.BA$4.run(BA.java:523)
    at anywheresoftware.b4a.BA.setActivityPaused(BA.java:408)
    at dia.stock.readbarc.afterFirstLayout(readbarc.java:106)
    at dia.stock.readbarc.access$000(readbarc.java:17)
    at dia.stock.readbarc$WaitForLayout.run(readbarc.java:80)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:158)
    at android.app.ActivityThread.main(ActivityThread.java:7229)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
** Activity (readbarc) Resume **

Here is the Activity which is part of an app of multiple activities, If I make an app just with this activity then there is no problem and works ok. If I comment out the 2 places where it writes to the screen then everything works , but I want to display this info.

If youone can point me in the right direction, it would help alot .

Thank you.....


B4X:
#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region
'Activity module
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim mResult As String
    Dim job2 As HttpJob
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 Button1 As Button
    Dim myABBarcode As ABZxing
    Dim Label1 As Label
    Dim MyPhone As PhoneId
    Dim lbl As Label
   
End Sub

Sub Read_Barcode_update
    lbl.Initialize("Label1")
    Activity.LoadLayout("peterbarcodetest")
    Label1.Text = mResult
   
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)
   
End Sub

Sub Button1_Click
    myABBarcode.ABGetBarcode("myabbarcode", "")
End Sub

Sub myABBarcode_BarcodeFound (barCode As String, formatName As String)

    Label1.Text = barCode
    mResult = barCode
    job2.Initialize("Job2", Me)
    job2.PostString("http://www.******.co.za/sen_post.php","Barc="&mResult&"&Location=DBN&User="&MyPhone.GetSimSerialNumber&"")
End Sub

Sub myABBarcode_Canceled()
    Label1.Text = "Canceled"
    mResult = "Canceled"
   
End Sub

Sub JobDone (Job As HttpJob)
    Log("JobName = " & Job.JobName & ", Success = " & Job.Success)
    If Job.Success = True Then
        Label1.Text = "updated"
    Else
        Log("Error: " & Job.ErrorMessage)
        ToastMessageShow("Error: " & Job.ErrorMessage, True)
    End If
    Job.Release
End Sub
 

MarcoRome

Expert
Licensed User
Longtime User
java.lang.RuntimeException: Object should first be initialized (Label).



B4X:
lbl.Initialize("Label1")
    Activity.LoadLayout("peterbarcodetest")
    Label1.Text = mResult

B4X:
  lbl.Initialize("lbl")
    Activity.LoadLayout("peterbarcodetest")
    lbl.Text = mResult
 
Upvote 0

Peter Lewis

Active Member
Licensed User
Longtime User
B4X:
lbl.Initialize("Label1")
    Activity.LoadLayout("peterbarcodetest")
    Label1.Text = mResult

B4X:
  lbl.Initialize("lbl")
    Activity.LoadLayout("peterbarcodetest")
    lbl.Text = mResult


Hi

Should I have both of these in my code ?
The only Label in the .bal file is Label1

I added the lbl afterwards looking through the forum to see if it would solve it , but it did not.

I am now very confused also the fact they are in 2 different code windows , tells me that they should no co-exist and I should choose one of them , THE FIRST one is what I have got already and it does not work

Thx
 
Upvote 0

MarcoRome

Expert
Licensed User
Longtime User
i see in you code this:

B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
  
    Dim Button1 As Button
    Dim myABBarcode As ABZxing
    Dim Label1 As Label '<---- HERE 
    Dim MyPhone As PhoneId
    Dim lbl As Label
  
End Sub

Sub Read_Barcode_update
    lbl.Initialize("Label1") '<---- HERE / DELETE THIS if isnt necessary
    Activity.LoadLayout("peterbarcodetest")
    Label1.Text = mResult
  
End Sub

Are you sure that in layout peterbarcodetest do you have Label1 ??
 
Upvote 0

JordiCP

Expert
Licensed User
Longtime User
If Label1 is part of your .bal, it will be initialized when you call LoadLayout, and can only be used after that

B4X:
Sub Read_Barcode_update
   Activity.LoadLayout("peterbarcodetest")
   Label1.Text = mResult
End Sub

Edit: Collision detected ;)
 
Upvote 0

Peter Lewis

Active Member
Licensed User
Longtime User
If Label1 is part of your .bal, it will be initialized when you call LoadLayout, and can only be used after that

B4X:
Sub Read_Barcode_update
   Activity.LoadLayout("peterbarcodetest")
   Label1.Text = mResult
End Sub

Edit: Collision detected ;)
it is part of peterbarcodetest.bal
 

Attachments

  • peterbarcodetest.bal
    2.2 KB · Views: 234
Upvote 0

Peter Lewis

Active Member
Licensed User
Longtime User
i see in you code this:

B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
 
    Dim Button1 As Button
    Dim myABBarcode As ABZxing
    Dim Label1 As Label '<---- HERE
    Dim MyPhone As PhoneId
    Dim lbl As Label
 
End Sub

Sub Read_Barcode_update
    lbl.Initialize("Label1") '<---- HERE / DELETE THIS if isnt necessary
    Activity.LoadLayout("peterbarcodetest")
    Label1.Text = mResult
 
End Sub

Are you sure that in layout peterbarcodetest do you have Label1 ??


Yes it is.

What I did when I combined the Activities was initially do what I wanted in seperate Apps.

I then went to the main app and created a new Activity whuich I named and copied the activity from the app that was working into that Activity Tab. I then went and added the libruaries that were used in the stand alone app to the new one as well as all the .bal files and any other files that were missing.

In the LOG of the combined app , I get NO errors. I only get errors when I compile and run the app

I did see somewhere that this might be a bug ??

Thanks

Peter
 
Upvote 0

Peter Lewis

Active Member
Licensed User
Longtime User
I also have just tried something different as there is a Label1 on the Start screen and I thought that might be causing a problem so I changed it in this Activity and the designer to Label100.

Same problem.
 
Upvote 0

Peter Lewis

Active Member
Licensed User
Longtime User
I don't see any Activity_create sub in the code you posted. Is it right?
I took it out because it was causing problems when I did a callsub.. the error was something about invalid signature.

After hours of trying to find a solution for it, I changed the name and called that and it worked...except for this issue.
 
Upvote 0

JordiCP

Expert
Licensed User
Longtime User
I guess the activity is Created internally but the needed B4A Sub which instantiates the global vars is not found and thus not executed.

That's the cause of that error.

Don't change your callback and try this code
B4X:
Sub Activity_Create(FirstTime as Boolean)  '<--Just add back this
   Activity.LoadLayout("peterbarcodetest")
End Sub

Sub Read_Barcode_update
   Label1.Text = mResult
End Sub
 
Upvote 0

Peter Lewis

Active Member
Licensed User
Longtime User
Yes, That seemed to have worked and No errors.

Now I am starting to understand that each of these Activities must have the SUB create.

THANK YOU !!!!!!
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
You are using Label1 in your layout but you want to use lbl.
Additionally in some modules you removed the sub activity_create. And, in fact you did remove the loadlayout from the place where it should be.
 

Attachments

  • intronew.zip
    117.9 KB · Views: 298
Upvote 0

Peter Lewis

Active Member
Licensed User
Longtime User
You are using Label1 in your layout but you want to use lbl.
Additionally in some modules you removed the sub activity_create. And, in fact you did remove the loadlayout from the place where it should be.
I only tried lbl as I has seen it in some code somewhere along the hours of going thru the Forum and searching. I must still get my head around the syntax and structures of this programming enviroment.

Thank you
 
Upvote 0
Top