confused with Activity_Resume and Activity_Pause

T4r4ntul4

Member
Licensed User
Longtime User
hey all,

if i rotate the device it resets everything to start, i experimented with a few things but iam coming nowhere.

can someone push me in the right direction? this is btw code from the tutorial guess, but with my own tests.

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.
   
   
   Type StateType(lab1 As String, lab4 As Int, lab6 As Int, lab7 As String, edit1 As Int, nmbr As Int, att As Int, lnr As Int)
   Dim State As StateType
   
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 MyNumber As Int
   Dim EditText1 As EditText 'will hold a reference to the view added by the designer
   Dim Label1 As Label
   Dim Label4 As Label
   Dim Label6 As Label
   Dim Label7 As Label
   Dim attempt As Int
   Dim lastnr As Int
   
   
End Sub

Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout("Layout1") 'Load the layout file.
   MyNumber = Rnd(1, 100) 'Choose a random number between 1 to 99
   attempt = 0
   lastnr = 0
   Label7.Text = ""
   Label4.Text = ""
   Label6.Text = ""
   Label1.Text = ""
   EditText1.Text = ""
End Sub

Sub Button1_Click
    If EditText1.Text > MyNumber Then
      Label1.Text = "Lower"
    Else If EditText1.Text < MyNumber Then
      Label1.Text = "Higher"
    Else
      Label7.Text = "yay! you did it!"
      Label1.Text = ""
    End If
    EditText1.SelectAll
   attempt = attempt + 1
   Label4.Text = attempt 'aantal attempts
   Label6.Text = EditText1.Text 'last nr weergave
End Sub

Sub Button2_Click 'reset alles, alles naar beginwaardes inc new raad getal
   attempt = 0
   lastnr = 0
   MyNumber = Rnd(1, 100)
   Label7.Text = ""
   Label4.Text = ""
   Label6.Text = ""
   Label1.Text = ""
   EditText1.Text = ""
   
End Sub

Sub ResetState
   State.edit1 = ""
   State.lab1 = ""
   State.lab4 = ""
   State.lab6 = ""
   State.lab7 = ""
End Sub


Sub Activity_Resume
   'dit gaat goed?
   attempt = State.att
   lastnr = State.lnr
   MyNumber = State.nmbr

   'hier gaat het fout...
'   Label7.Text = State.lab7
'   Label4.Text = State.lab4 'attempts
'   Label6.Text = State.lab6
'   Label1.Text = State.lab1
'   EditText1.Text = State.edit1
   
End Sub

Sub Activity_Pause (UserClosed As Boolean)
   'test
   If UserClosed Then
      ResetState
   Else
      State.lab7 = Label7.Text
      State.lab4 = Label4.Text
      State.lab6 = Label6.Text
      State.lab1 = Label1.Text
      State.edit1 = EditText1.Text
      State.nmbr = MyNumber
   End If
   
End Sub
 

T4r4ntul4

Member
Licensed User
Longtime User
When you rotate the device the layout is recreated. You should set the labels and edittext text property in Activity_Resume. It is not enough to set the variables.


yes that should be the problem... but, i dont know how?
 
Upvote 0

T4r4ntul4

Member
Licensed User
Longtime User
if i do uncomment these lines i get a error on line 92 with the:

B4X:
   Label7.Text = State.lab7
   Label4.Text = State.lab4 'attempts
   Label6.Text = State.lab6
   Label1.Text = State.lab1
   EditText1.Text = State.edit1

and the global variables box in the ide says:
made a picture couldnt copy and paste it:
 

Attachments

  • error1.JPG
    error1.JPG
    44.2 KB · Views: 206
Upvote 0

T4r4ntul4

Member
Licensed User
Longtime User
line 92: Label7.Text = State.lab7

not really clear for me what the error is trying to say...

logcat information:

B4X:
LogCat connected to: emulator-5554
** Activity (main) Create, isFirst = true **


** Activity (main) Resume **


main_activity_resume (B4A line: 92)
Label7.Text = State.lab7

java.lang.NullPointerException
   at anywheresoftware.b4a.objects.TextViewWrapper.setText(TextViewWrapper.java:34)
   at dppro.guess.main._activity_resume(main.java:342)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:521)
   at anywheresoftware.b4a.BA.raiseEvent2(BA.java:105)
   at anywheresoftware.b4a.BA.raiseEvent(BA.java:89)
   at dppro.guess.main.afterFirstLayout(main.java:90)
   at dppro.guess.main.access$100(main.java:16)
   at dppro.guess.main$WaitForLayout.run(main.java:72)
   at android.os.Handler.handleCallback(Handler.java:587)
   at android.os.Handler.dispatchMessage(Handler.java:92)
   at android.os.Looper.loop(Looper.java:123)
   at android.app.ActivityThread.main(ActivityThread.java:4627)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:521)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
   at dalvik.system.NativeStart.main(Native Method)
java.lang.NullPointerException

included the export zip, if you want the file yourself.
 

Attachments

  • guess_test.zip
    6.6 KB · Views: 187
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Activity_Resume is called right after Activity_Create.
You didn't initialize the State variable and you didn't set its values.

To fix it you should call ResetState when FirstTime = True:
B4X:
Sub Activity_Create(FirstTime As Boolean)
   If FirstTime Then
      ResetState
   End If

You will also need to fix ResetState as some of the fields are integers:
B4X:
Sub ResetState
   State.edit1 = 0
   State.lab1 = ""
   State.lab4 = 0
   State.lab6 = 0
   State.lab7 = ""
End Sub

StateManager will solve all of these issues for you.
 
Upvote 0
Top