Object should first be initialized

scrat

Active Member
Licensed User
Longtime User
Hello,

I have a strange error
When I start my program from the phone (Standalone) I have an error message:

An error has occured .........
java.lang.runtimeexception:
Object should first be initialized (label).

if I start the program again I no longer error and everything works perfectly.

Labels are created with the designer

If I run the application in emulator or with B4A bridge i have no message.

This is my first application and I do something wrong


Thanks
 

kickaha

Well-Known Member
Licensed User
Longtime User
Do you run any code before you load the layout?

If possible post the code up here for people to have a look at.
 
Upvote 0

scrat

Active Member
Licensed User
Longtime User
Thanks for your reply

It seems that the accelerometer is read before the creation of labels

B4X:
Sub Process_Globals
Dim Accelerometer As PhoneAccelerometer
End Sub

Sub Globals
Dim Titre1 As ImageView
Dim Titre2 As ImageView
Dim LabelX As Label
Dim LabelY As Label
Dim LabelZ As Label
Dim LabelTot As Label   
Dim LabelXmax As Label
Dim LabelXmin As Label
Dim LabelYmax As Label
Dim LabelYmin As Label
Dim LabelZmax As Label
Dim LabelZmin As Label
Dim LabelGmax As Label
Dim LabelGmin As Label
Dim Xmax As Float, Xmin As Float, Ymax As Float, Ymin As Float, Zmax As Float, Zmin As Float, Amax As Float, Amin As Float   
Dim Calibration As Boolean
Dim NbCount As Int
Dim CurrentCount As Int
Dim MaxScale As Float
Dim Raz As Button
Dim BtnQuit As Button
Dim Secu As Int
End Sub

Sub Activity_Create(FirstTime As Boolean)
If firstTime Then
Activity.LoadLayout("layGui")
MaxScale=1
NbCount=50
CurrentCount=0


'calibration si le fichier calib.txt n'existe pas
If File.Exists(File.DirDefaultExternal,"calib.txt")=False Then
   'si on ne peut pas ecrire sur la sd
   'pas de calibration
   If File.ExternalWritable = False Then
   Msgbox("Ecriture impossible sur la SD card.", "Pas de calibration possible")
   Calibration=False
   Return
   Else
   Msgbox("Poser votre GSM à plat et presser OK","Calibration")
   Calibration=True
   End If
Else
MaxScale=File.ReadString(File.DirDefaultExternal,"calib.txt")
End If


End If
End Sub

Sub Activity_Resume
Accelerometer.StartListening("Accelerometer") 
End Sub




Sub Activity_Pause (UserClosed As Boolean)
Accelerometer.StopListening 
End Sub




Sub Accelerometer_AccelerometerChanged (X As Float, Y As Float, Z As Float)

Dim Ax As Float
Dim Ay As Float
Dim Az As Float


Ax=(X/9.81)/MaxScale
Ay=(Y/9.81)/MaxScale
Az=(Z/9.81)/MaxScale

'Calibration de l'accelero
If Calibration=True Then
Calibrate(AX,AY,AZ)
Return
End If
   
Atot=Sqrt(Ax*Ax+Ay*Ay+Az*Az)

SetMiniMax(Ax,Ay,Az,Atot)


LabelX.Text=" X: " & FormatVal(Ax,3,True)
LabelY.Text=" Y: " & FormatVal(AY,3,True)
LabelZ.Text=" Z: " & FormatVal(Az,3,True)
LabelTot.Text= FormatVal(Atot,1,False) & " G"

LabelXmax.Text="Xmax: " & FormatVal(Xmax,2,True)
LabelYmax.Text="Ymax: " & FormatVal(Ymax,2,True)
LabelZmax.Text="Zmax: " & FormatVal(Zmax,2,True)
LabelXmin.Text="Xmin: " & FormatVal(Xmin,2,True)
LabelYmin.Text="Ymin: " & FormatVal(Ymin,2,True)
LabelZmin.Text="Zmin: " & FormatVal(Zmin,2,True)
LabelGmin.Text=FormatVal(Amin,1,False) & " Gmin"
LabelGmax.Text=FormatVal(Amax,1,False) & " Gmax"

End Sub
 
Upvote 0

scrat

Active Member
Licensed User
Longtime User
Thanks

problem solved

I thought this event was the equivalent of "onformcreate" that are used in Pascal
and "resume activity" was "on show"
I'll get a documentation !!!
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0

andrewtheart

Member
Licensed User
Longtime User
OK. I am getting the same error when calling LoadLayout (and not checking if it's the FirstTime) - it is saying that I didn't initialize a WebView that is in the layout I am trying to load.

I can send you my code in a PM if you want.
 
Upvote 0

bergapappa

Member
Licensed User
Longtime User
FirstTime

The error in this thread was that the layout was only loaded when FirstTime is True. FirstTime is true only when the process is created. The activity can be destroyed and recreated many times while the process stays alive. See this tutorial: http://www.b4x.com/forum/basic4andr...87-android-process-activities-life-cycle.html

Erel - Do you mean It is always wrong to loadlayout in the firstTime check?
My app does only support portrait and I'm not sure why else it would be destroyed and recreated?

If the activity is destroyed and the user restarts the app, the FitstTime will be true, wont it?


Regards

Bergapappa
 
Upvote 0

afagcaoili

Member
Licensed User
Longtime User
I could open another thread but my issue is similar

Hi,

I have just a sample program nothing special just a layout (created in the designer) with 3 imageviews.

It spit out the error below when it resumes. Initial creation works but when it pauses (UserClosed True or False) will give the error.

One Thing I noticed is that if I only have up to 2 of the imageviews populated with an image file, it seems to work but by the time I put an image file to the 3rd or probably more imageviews then it gives me this error.

B4X:
java.lang.RuntimeException: Object should first be initialized (ImageView).

Here is the code which is simple.

B4X:
'Activity module
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 ImageView1 As ImageView
   Dim ImageView2 As ImageView
   Dim ImageView3 As ImageView
End Sub

Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout("main")
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub
 
Upvote 0

margret

Well-Known Member
Licensed User
Longtime User
If you are not using LoadBitmapSample, you should use it to fill the imageviews. Sounds like with the third image the os is killing the activity to free resources and when your activity resumes the objects have been removed. Make a much smaller version of your images and try it or use the LoadBitmapSample method.
 
Upvote 0

NJDude

Expert
Licensed User
Longtime User
Ok, this is your problem:
B4X:
Sub Activity_Resume
    Activity.LoadLayout("main")
End Sub

The layouts are loaded ONLY in Activity_Create, you need to remove that line, same thing for the line Activity.RemoveView in Activity_Pause
 
Upvote 0

afagcaoili

Member
Licensed User
Longtime User
Thanks NJDude, I appreciate your quick response.

It did took it out but it still showed the same error.

Surprisingly enough, I did try to set the image programmatically using LoadBitmapSample to follow margret's suggestion and took out the image file definition in the layout. This solution worked!

Thanks a lot guys for your time. This community rocks! I am just here less than a week and I am very much impressed on you guys' response and abilities!

Keep it up. GBU all! :sign0098:

Regards,

Arnold
 
Upvote 0
Top