Android Question A NewBie at B4A

pokhraj_d

Member
Hello All,
I am newbie at B4A and Not able to understand the below error.
When first time I click on Check button after compile it is showing the correct result, but after that every time the app showing the result is wrong.
Am I missing something. Please suggest.
Below is my code
B4X:
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 Button1 As Button
    Private Label3 As Label
    Private lblnumber1 As Label
    Private lblnumber2 As Label
    Private lblResult As Label
    Private txt1 As EditText
    Public num1,num2 As Int
    Private btnNumcreate As Button
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("Layout1")
    Activity.LoadLayout("calculator")
    New
End Sub

Sub New
    num1=Rnd(1,10)
    num2=Rnd(1,10)
    lblnumber1.Text=num1
    lblnumber2.Text=num2
    lblResult.Text=""
End Sub
Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Button1_Click()
    If txt1.Text="" Then
        lblResult.Text=""
        Msgbox("No Result Entered!","ERROR")
    Else
        CheckResult
    End If
End Sub

Sub CheckResult
    If txt1.Text=(num1+num2) Then
        lblResult.Text="Correct!"
    Else
        lblResult.Text="Wrong!"
    End If
End Sub

Sub btnNumcreate_Click()
    lblnumber1.Text=Rnd(1,10)
    lblnumber2.Text=Rnd(1,10)
    lblResult.Text=""
    txt1.Text=""
End Sub
 

klaus

Expert
Licensed User
Longtime User
I think that the problem is here:
B4X:
Sub btnNumcreate_Click()
    lblnumber1.Text=Rnd(1,10)
    lblnumber2.Text=Rnd(1,10)
    lblResult.Text=""
    txt1.Text=""
End Sub
It should be:
B4X:
Sub btnNumcreate_Click()
    num1=Rnd(1,10)
    num2=Rnd(1,10)
    lblnumber1.Text=num1
    lblnumber2.Text=num2
    lblResult.Text=""
    txt1.Text=""
End Sub
Or simply:
B4X:
Sub btnNumcreate_Click()
    New
End Sub
 
Last edited:
Upvote 0
Top