Android Question [solved] Error Label should first be initialized re-launching

SimonInCanada

Member
Licensed User
My App runs fine the first time it is launched. If use the back button to dismiss it, if I then re-launch it from its icon or from the currently running list, it is launched with FirstTime = False and subsequently the first time I try to update a Label.Text, it gives Error label should first be initialised, and "Unfortunately, APPNAME has stopped on the screen". Next time, it launches okay. This is 100% repeatable.

Having read various tutorials etc about the App life cycle I tried calling Activity.Finish in various ways to no avail. I have searched all morning on the forums without finding this exact topic. Please note: I've only been using B4a for a couple of weeks.

I include the source in a zip in case it's obvious (e.g. a variable declared in the wrong place). For what it's worth, the App is a look-alike of one I wrote last year in Embarcadero Delphi / Firemonkey. The phone is used to replace an old Windows PDA and gets a simple web page from our embedded system then parses it and puts it on the screen.

Thanks in advance for any help

Simon

Output is from a Release copy of the program...

Logger connected to: BCom Mobile B.V. EX-SM14
--------- beginning of /dev/log/main
--------- beginning of /dev/log/system
Process Globals
** Service (starter) Create **
** Service (starter) Start **
Globals
** Activity (main) Create, isFirst = true **
Activity_Create
** Activity (main) Resume **
Activity_Resume
Timer1_Tick
** Service (httputils2service) Create **
** Service (httputils2service) Start **
JobName = Job1, Success = true
JobName = Job1, Success = true
JobName = Job1, Success = true
JobName = Job1, Success = true
JobName = Job1, Success = true
** Activity (main) Pause, UserClosed = true **
Activity_Pause
Globals
** Activity (main) Create, isFirst = false **
Activity_Create
** Activity (main) Resume **
Activity_Resume
main_updatewifi (java line: 957)
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 b4a.example.main._updatewifi(main.java:957)
at b4a.example.main._timer2_tick(main.java:854)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:179)
at anywheresoftware.b4a.objects.Timer$TickTack.run(Timer.java:105)
at android.os.Handler.handleCallback(Handler.java:808)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5299)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:829)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:645)
at dalvik.system.NativeStart.main(Native Method)
 

Attachments

  • Wifi App.zip
    17.9 KB · Views: 259

Star-Dust

Expert
Licensed User
Longtime User
try

if LabelName.IsInitialize=False then
' Initialize
End if

However I gave error and locks in this line, I do not by initialization error.

B4X:
row:198
TabHostEx.setTabTitle(TabHost1, "Berth 1/3", 1)
 
Last edited:
Upvote 0

SimonInCanada

Member
Licensed User
try

if LabelName.IsInitialize=False then
' Initialize
End if

However I gave error and locks in this line, I do not by initialization error.

B4X:
row:198
TabHostEx.setTabTitle(TabHost1, "Berth 1/3", 1)

Hi,

If I initialize all the labels declared by the designer in Sub Globals, from Activity_Create if FirstTime = True, then the app works without error. There are also Panels and the TabHost which are declared in Sub Globals but don't create a problem.

The compiler really does not like me doing this and produces Warning # 20 for each Label. That's why I didn't try it before posting.

I'm using TabHostExtras V2.20 to add functionality to the standard TabHost, so you need this library to actually compile the program (TabHostEx.setTabTitle(TabHost1, "Berth 1/3", 1)).

Can anybody explain why I am having to do this as to work around? The phone by the way is Android 4.4.2 (it's a ruggedized ATEX phone and these are always a bit behind the times with their Android versions).

Thanks

Simon
 
Upvote 0

SimonInCanada

Member
Licensed User
In addition,

After I added all the commands to initialise the labels as I posted above, everything seemed to work okay at first but then I started getting the same issue accessing three Panels, which I fixed in the same way). Then it started giving me an error with the command that Star-Dust mentioned: TabHostEx.setTabTitle(TabHost1, "Berth 1/3", 1). This was solved by moving its declaration to Sub Globals from Process_Globals.

Now it is all working :)
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
I only gave that mistake and not always. I had to go out and retrieve it several times and then gave me that error....

Try to always initialize regardless knows FirstTime, or use StartService to initialize.
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
I had read that you solved, good. Put [SOLVED] in the thread title
 
Upvote 0

SimonInCanada

Member
Licensed User
I only gave that mistake and not always. I had to go out and retrieve it several times and then gave me that error....

Try to always initialize regardless knows FirstTime, or use StartService to initialize.

Everything did seem okay for a while, then without changing the app, I got the problem in the line with TabHostEx.setTabTitle(TabHost1, "Berth 1/3", 1). I thought I had fixed this by moving the declaration of TabHostEx as explained above. Now it is happening every second time as described in my original post, just with TabHostEx.
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
Put a check on Activity_Resume
If TabHost is not initialized you call the procedure that initializes
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
I think that every time the app goes on break for reasons that I can not explain resets the interface and must be initialized again to resume. Normally it is not so, but a few items are re-initialized event Resume.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
I think that every time the app goes on break for reasons that I can not explain resets the interface and must be initialized again to resume. Normally it is not so, but a few items are re-initialized event Resume.
That's not correct. It is a mistake to initialize views in Activity_Resume.
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
In fact, as Erel says it would be wrong, but it remains strange that the error signals. Usually it initialize Camera in the resume, but no views.
 
Last edited:
Upvote 0
Top