Android Question java.lang.IllegalStateException: Interval must be larger than 0.

powerino

Active Member
Licensed User
Hello, I have this problem with "BannerAd"... Error is here:

Sub Activity_Resume

timerInter.Enabled=True

End Sub

Somebody cna help me?

thanks
 

powerino

Active Member
Licensed User
B4X:
#Region  Project Attributes 
    #ApplicationLabel: Math Test
    #VersionCode: 2
    #VersionName: 1.1
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: portrait
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes 
    #FullScreen: True
    #IncludeTitle: False
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

    Dim timer1 As Timer
    Dim stu As StringUtils
    Dim timerInter As Timer

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 BannerAd As AdView
    Private IAd As InterstitialAd
   
    ' per accedere alla memora usando libreria RUNTIME PERMISSION
    Private rp As RuntimePermissions
   
    Private introLabel As Label
    Private mathLabel As Label
    Private okButton As Button
    Private resultField As EditText
    Private titleImage As ImageView
    Dim myImage As Bitmap
    Dim testLevel As Int
    Dim resultInt As Int
    Private errorsLabel As Label
    Private progressTest As ProgressBar
    Private questionNumbLabel As Label
    Dim indexMax As Int
    Dim errorsTotal As Int
    Private backgroundImage As ImageView   
    Private timeLabel As Label
    Dim timePassed As Int
   
    Dim minutes As Int
    Private Panel1 As Panel
    Private panelTitleLAbel As Label
    Private panelLAbel As Label
    Private panelButton As Button
    Dim dayTodayString As String
    Dim monthTodayString As String
    Dim yearTodayString As String
   
    Dim listResult(100) As String
   
    Private listButton As Button
   
    Private dateLabel As Label
    Dim monthString As String
   
    Private infoButton 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("Main")
   
   
    Dim Folder As String ="MathTest"
    File.MakeDir(File.DirRootExternal,Folder)
    Dim FullPath As String=File.Combine(File.DirRootExternal,Folder)
   
    rp.CheckAndRequest(rp.PERMISSION_WRITE_EXTERNAL_STORAGE)  '
    Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
    If Result = False Then
        MsgboxAsync("No permission to access external storage", "")
        Return
    Else
        Dim lstResults As List
        lstResults.Initialize
        lstResults.Add(1)
        lstResults.Add(2)
        lstResults.Add(3)
        Dim FileName As String= "Results.txt"
        Log(FullPath)
        File.WriteList(FullPath,FileName,lstResults)
    End If
   

    FindDate
    CalculateMonth
   
    dateLabel.Text=dayTodayString & " " & monthString & " " & yearTodayString
   
    Panel1.Visible=False
   
    '1000 = 1 secondo
    timer1.Initialize("timer1", 1000)
    timeLabel.TextColor=Colors.Red
   
    'variabile dei secondi e minuti x timer
    timePassed=0
    minutes=0
    timeLabel.Text= "Tempo: 00m:00s"
           
    'setto immagine background
    backgroundImage.Bitmap=LoadBitmap(File.DirAssets, "mathPaper.png")
   
    errorsTotal=0
    errorsLabel.Text="Errori: " & errorsTotal
   
    'imposto la progressbar
    progressTest.Progress=0
    indexMax = 10
   
    'assegno il valore massimo della progressbar
    setMaxProgressBar
   
    'cambio il colore della label
    resultField.Color = Colors.Blue
    resultField.TextColor= Colors.White
    resultField.Hint="Inserisci la tua risposta"
    resultField.HintColor=Colors.Gray
   
    introLabel.TextColor= Colors.Red
    mathLabel.TextColor= Colors.Red
    errorsLabel.TextColor= Colors.Blue
    questionNumbLabel.TextColor= Colors.Blue
   
   
    'spessore progressbar
    Dim gd As GradientDrawable
    gd.Initialize("LEFT_RIGHT", Array As Int(Colors.RGB(211,223,233), Colors.RGB(58,153,233)))
    gd.CornerRadius=3dip
    SetProgressDrawable(progressTest,gd)
       
    'imposto il livello 0 del test
    testLevel=0
    questionNumbLabel.Text = "Livello numero: " & testLevel
    'okButton.Text="OK"
    SetButtonTextSize(okButton,"OK")
    SetButtonTextSize(panelButton,"Chiudi")
   
   
    'setto il titolo con la sua immagine
    titleImage.Bitmap=LoadBitmap(File.DirAssets, "title.png")
    Activity.Title = "Math Test"
   
   
    'info per utente
    'introLabel.Text = "Clicca ok per cominciare"
    SetLabelSize(introLabel,"Clicca ok per cominciare")
   
    'setto il pulsante con la sua immagine
    myImage.Initialize(File.DirAssets, "bottone-blu.png")
    okButton.SetBackgroundImage(myImage)
    panelButton.SetBackgroundImage(myImage)
   
    myImage.Initialize(File.DirAssets, "infoButton.png")
    infoButton.SetBackgroundImage(myImage)
   
    myImage.Initialize(File.DirAssets, "list.png")
    listButton.SetBackgroundImage(myImage)
   
    'setto lo sfondo del panel
    myImage.Initialize(File.DirAssets, "panelBack.png")
    Panel1.SetBackgroundImage(myImage)
   
    panelTitleLAbel.TextColor=Colors.Red
    panelLAbel.TextColor=Colors.Blue
   
    'lancio il banner
    CaricaBanner
   
    'per reclame interstitial
    timerInter.Initialize("timerInter",5000)
    timerInter.Enabled=True

End Sub

Sub Activity_Resume
   
    'If timerInter.Enabled=False Then
       
        'timerInter.Initialize("timerInter",2000)
        timerInter.Enabled=True
       
    'Else
       
        'timerInter.Initialize("timerInter",5000)
           
    'End If
   

   
   
End Sub

Sub timerInter_Tick
   
    caricaIterstitial
    timerInter.Enabled=False
   
End Sub


Sub caricaIterstitial
   
    If IAd.Ready Then IAd.Show

End Sub

Sub IAD_AdClosed
   
    IAd.LoadAd 'preparo il prossimo messaggio pubblicitario
   
End Sub




Sub Activity_Pause (UserClosed As Boolean)

End Sub



Sub CaricaBanner
   
    BannerAd.Initialize2("BannerAd", "ca-app-pub-3940256099942544/6300978111", BannerAd.SIZE_SMART_BANNER) 'test banner
    'BannerAd.Initialize2("BannerAd", "ca-app-pub-7754341356106686/9095773896", BannerAd.SIZE_SMART_BANNER)
    Dim height As Int
    If GetDeviceLayoutValues.ApproximateScreenSize < 6 Then
        'phones
        If 100%x > 100%y Then height = 32dip Else height = 50dip
    Else
        'tablets
        height = 90dip
    End If
    Activity.AddView(BannerAd, 0dip, 100%y - height, 100%x, height)
    BannerAd.LoadAd
    IAd.Initialize("iad", "ca-app-pub-3940256099942544/1033173712")'test interstitial
    'IAd.Initialize("iad", "ca-app-pub-7754341356106686/1100490535")
    IAd.LoadAd
   

   
End Sub

Sub timer1_Tick
    Dim minuteString As String
    Dim secondString As String
   
   
    timePassed=timePassed +1
    If timePassed = 60 Then
       
        minutes = minutes+1
        timePassed=0
               
    End If
   
    If minutes <10 Then
        minuteString= "0" & minutes
    Else 
        minuteString= minutes
    End If
   
    If timePassed <10 Then
        secondString= "0" & timePassed
    Else
        secondString= timePassed
    End If
   
    timeLabel.Text= "Tempo: " & minuteString & "m:" & secondString & "s"
       
End Sub


Sub CalculateMonth
   
    If monthTodayString = 1 Then
        monthString="Gennaio"
    else if monthTodayString = 2 Then
        monthString="Febbrai"
    else if monthTodayString = 3 Then
        monthString="Marzo"
    else if monthTodayString = 4 Then
        monthString="Aprile"
    else if monthTodayString = 5 Then
        monthString="Maggio"
    else if monthTodayString = 6 Then
        monthString="Giugno"
    else if monthTodayString = 7 Then
        monthString="Luglio"
    else if monthTodayString = 8 Then
        monthString="Agosto"
    else if monthTodayString = 9 Then
        monthString="Settembre"
    else if monthTodayString = 10 Then
        monthString="Ottobre"
    else if monthTodayString = 11 Then
        monthString="Novembre"
    else if monthTodayString = 12 Then
        monthString="Dicembre"
    End If
   
End Sub


Sub SetProgressDrawable(p As ProgressBar, drawable As Object)
    Dim r As Reflector
    Dim clipDrawable As Object
    clipDrawable = r.CreateObject2("android.graphics.drawable.ClipDrawable", _
      Array As Object(drawable, Gravity.LEFT, 1), _
      Array As String("android.graphics.drawable.Drawable", "java.lang.int", "java.lang.int"))
    r.Target = p
    r.Target = r.RunMethod("getProgressDrawable") 'Gets the layerDrawable
    r.RunMethod4("setDrawableByLayerId", _
      Array As Object(r.GetStaticField("android.R$id", "progress"), clipDrawable), _
      Array As String("java.lang.int", "android.graphics.drawable.Drawable"))
End Sub


Sub setMaxProgressBar ' imposto il max progressbar
   
    Dim rflBar As Reflector
   
    rflBar.Target = progressTest
    rflBar.RunMethod2("setMax", indexMax, "java.lang.int")
   
End Sub


Sub createCalculation
   
    Dim num1 As Int
    Dim num2 As Int
    Dim num3 As Int
    Dim num4 As Int
   
   
    If testLevel = 1 Then
        num1=Rnd(1,101)
        num2=Rnd(1,101)
        'mathLabel.Text = num1 & " + " & num2
        SetLabelSize(mathLabel, num1 & " + " & num2)
        resultInt = num1+num2
   
    Else If testLevel = 2 Then
        num1=Rnd(1,101)
        num2=Rnd(1,101)
        num3=Rnd(1,101)
        'mathLabel.Text = num1 & " + " & num2 & " - " & num3
        SetLabelSize(mathLabel, num1 & " + " & num2 & " - " & num3)
        resultInt = num1+num2-num3
   
    Else If testLevel = 3 Then
        num1=Rnd(1,101)
        num2=Rnd(1,101)
        num3=Rnd(1,101)
        'mathLabel.Text = num1 & " + " & num2 & " - " & num3
        SetLabelSize(mathLabel, "-"& num1 & " + " & num2 & " - " & num3)
        resultInt = -num1+num2-num3
       
    Else If testLevel = 4 Then
        num1=Rnd(1,10)
        num2=Rnd(1,10)
        num3=Rnd(1,101)
        'mathLabel.Text = "(" & num1 & " X " & num2 & ") - " & num3
        SetLabelSize(mathLabel, "(" & num1 & " X " & num2 & ") - " & num3)
        resultInt = num1*num2-num3
       
    Else If testLevel = 5 Then
        num1=Rnd(1,10)
        num2=Rnd(1,10)
        num3=Rnd(1,101)
        'mathLabel.Text = "(" & num1 & " X " & num2 & ") - " & num3
        SetLabelSize(mathLabel, "(" & num1 & " X " & num3 & ") - " & num2 &" + " & num1)
        resultInt = num1*num3-num2+num1
   
    Else If testLevel = 6 Then
        num1=Rnd(1,10)
        num2=Rnd(1,10)
        num3=Rnd(1,101)
        'mathLabel.Text = "((" & num1 & " X " & num2 & ") X " & num1 & ") + " & num3
        SetLabelSize(mathLabel, "((" & num1 & " X " & num2 & ") X " & num1 & ") + " & num3)
        resultInt = num1*num2*num1+num3

    Else If testLevel = 7 Then
        num1=Rnd(1,10)
        num2=Rnd(1,5)
        num3=Rnd(1,101)
        'mathLabel.Text = "((" & num1 & " X " & num2 & ") X " & num1 & ") + " & num3
        SetLabelSize(mathLabel, "(" & num1 & " X " & num2 & " X " & num1 & ") - " & num3 & " + " & num1)
        resultInt = num1*num2*num1-num3+num1
       
    Else If testLevel = 8 Then
        num1=Rnd(1,10)
        num2=Rnd(1,10)
        num3=Rnd(1,10)
        num4=Rnd(1,101)
        'mathLabel.Text = "((" & num1 & " X " & num2 & ") - (" & num1 & " X " & num3 & ")) + " & num4 
        SetLabelSize(mathLabel, "((" & num1 & " X " & num2 & ") - (" & num1 & " X " & num3 & ")) + " & num4 )
        resultInt = num1*num2-num1*num3+num4
   
    Else If testLevel = 9 Then
        num1=Rnd(1,9)
        num2=Rnd(1,3)
        num3=Rnd(1,4)
        num4=Rnd(1,151)
        'mathLabel.Text = "((" & num1 & " ^ " & num2 & ") X " & num3 & ") - " & num4
        SetLabelSize(mathLabel, "((" & num1 & " ^ " & num2 & ") X " & num3 & ") - " & num4 )
        resultInt = Power(num1,num2)*num3-num4
   
    Else If testLevel = 10 Then
        num1=Rnd(1,12)
        num2=Rnd(1,4)
        num3=Rnd(1,5)
        num4=Rnd(1,201)
        'mathLabel.Text = "((" & num1 & " ^ " & num2 & ") X " & num3 & ") - " & num4 & " - " & num1
        SetLabelSize(mathLabel, "((" & num1 & " ^ " & num2 & ") X " & num3 & ") - " & num4 & " - " & num1 )
        resultInt = Power(num1,num2)*num3-num4-num1
       
       
    End If
   

    Log(resultInt)
   
   
End Sub

Sub okButton_Click
   
    Dim minuteString As String
    Dim secondString As String
    Dim erroreString As String
       
    If testLevel=0 Then
       
        testLevel=1
        questionNumbLabel.Text = "Livello numero: " & testLevel
        createCalculation
        timer1.Enabled=True 'faccio partire il timer
        'introLabel.Text = "Scrivi il risultato nella finestra blue e clicca OK"
        SetButtonTextSize(okButton,"OK")
        SetLabelSize(introLabel,"Scrivi il risultato nella finestra blue e clicca OK")
   
    Else
       
        'se è giusta
        If resultInt = resultField.Text Then
           
            'pulisco la edit text
            resultField.Text=""
           
            ToastMessageShow("risposta corretta!", True)
            testLevel = testLevel + 1
            questionNumbLabel.Text = "Livello numero: " & testLevel
            progressTest.Progress=progressTest.Progress + 1
           
            If testLevel < (indexMax+1) Then
                createCalculation
            Else
               
                If minutes=1 Then
                    minuteString = " minuto"       
                Else
                    minuteString = " minuti"
                End If
               
                If minutes=1 Then
                    secondString = " secondo"
                Else
                    secondString = " secondi"
                End If
               
                If errorsTotal=1 Then
                    erroreString = " errore"
                Else
                    erroreString = " errori"
                End If
               
                SetLabelSize( mathLabel,"")
                resultField.Text=""
                questionNumbLabel.Text = "Livello numero: "
                'okButton.Text="Nuovo Test"
                SetButtonTextSize(okButton,"Nuovo Test")
               
                okButton.Visible=False
               
                If errorsTotal=0 Then
                   
                    SetLabelSize(panelTitleLAbel, "Complimenti!")
                    SetLabelSize(panelLAbel, "Il test è terminato con " & indexMax & " risposte esatte senza commettere errori in "  & minutes & minuteString & " e " & timePassed & secondString & ". Sei bravissimo!")
               
                else if errorsTotal<3 Then
               
                    SetLabelSize(panelTitleLAbel, "Ben fatto")
                    SetLabelSize(panelLAbel, "Il test è terminato con " & indexMax & " risposte esatte e  " & errorsTotal & erroreString & " in " & minutes & minuteString & " e " & timePassed & secondString & ". Puoi fare di meglio!")
           
                Else
                    SetLabelSize(panelTitleLAbel, "Peccato!")
                    SetLabelSize(panelLAbel, "Il test è terminato con " & indexMax & " risposte esatte e  " & errorsTotal & erroreString & " (troppi) in " & minutes & minuteString & " e " & timePassed & secondString & ". Devi studiare di più!")
                       
                End If
               
                'egisterResults(minutes, timePassed, errorsTotal)
                RegisterResults(minutes, timePassed, errorsTotal)
               
                Panel1.Visible=True
               
                'Msgbox("Il test è terminato con " & indexMax & " risposte esatte e  " & errorsTotal & erroreString & " in " & minutes & minuteString & " e " & timePassed & secondString, "Complimenti")
               
               
                timer1.Enabled=False
                testLevel=0
                progressTest.Progress=0
                errorsTotal=0
                errorsLabel.Text="Errori: " & errorsTotal
                timePassed=0
                minutes=0
                timeLabel.Text= "Tempo: 00m:00s"
                'introLabel.Text = "Clicca ""Nuovo Test"" per cominciare"
                SetLabelSize(introLabel,"Clicca ""Nuovo Test"" per cominciare")
           
            End If
                   
        Else
           
            errorsTotal = errorsTotal +1
            errorsLabel.Text="Errori: " & errorsTotal
            If errorsTotal=1 Then
                erroreString = " errore"
            Else
                erroreString = " errori"
            End If
            Msgbox("La risposta " & testLevel & " è errata! Hai accumulato un totale di " & errorsTotal & erroreString,"Attenzione!")
           
        End If
   
    End If
   
End Sub


'questo serve a modificare la grandezza del testo nella label
Sub SetLabelSize(lbl As Label, txt As String)
    Dim dt As Float
    Dim limit = 0.5 As Float
    Dim h As Int
   
    lbl.Text = txt
    lbl.TextSize = 72
    dt = lbl.TextSize
    h = stu.MeasureMultilineTextHeight(lbl, txt)
    Do While dt > limit Or h > lbl.Height
        dt = dt / 2
        h = stu.MeasureMultilineTextHeight(lbl, txt)
        If h > lbl.Height Then
            lbl.TextSize = lbl.TextSize - dt
            lbl.TextSize = lbl.TextSize - 1 'ho aggiunto questo meno 1 se no a volte la scritta era grande e non si leggeva
        Else
            lbl.TextSize = lbl.TextSize + dt
            lbl.TextSize = lbl.TextSize - 1 'ho aggiunto questo meno 1 se no a volte la scritta era grande e non si leggeva
        End If
    Loop
End Sub
   

'questo serve a modificare la grandezza del testo nel bottone
Sub SetButtonTextSize(lbl As Button, txt As String)
    Dim dt As Float
    Dim limit = 0.5 As Float
    ' Dim limit = 1 As Float
    Dim h As Int
   
    lbl.Text = txt
    lbl.TextSize = 72
    dt = lbl.TextSize
    h = stu.MeasureMultilineTextHeight(lbl, txt)
    Do While dt > limit Or h > lbl.Height
        dt = dt / 2
        h = stu.MeasureMultilineTextHeight(lbl, txt)
        If h > lbl.Height Then
            lbl.TextSize = lbl.TextSize - dt
            lbl.TextSize = lbl.TextSize - 2 'ho aggiunto questo meno 2 se no era grande e andava fuori dal bottone
        Else
            lbl.TextSize = lbl.TextSize + dt
            lbl.TextSize = lbl.TextSize - 2 'ho aggiunto questo meno 2 se no era grande e andava fuori dal bottone
        End If
    Loop
End Sub



Sub panelButton_Click
    Panel1.Visible=False
    okButton.Visible=True
End Sub


Sub FindDate
   
    Dim year As Int = DateTime.GetYear(DateTime.Now)
    Dim month As Int = DateTime.GetMonth(DateTime.Now)
    Dim day As Int = DateTime.GetDayOfMonth(DateTime.Now)
    'Dim hour As Int = DateTime.GetHour(DateTime.bow)
   
   
   
    monthTodayString=month
    dayTodayString=day
    yearTodayString=year
   
    'Log(dayTodayString  & "/" & monthTodayString & "/" & yearTodayString)
   
End Sub



Sub RegisterResults (minutesInt As Int, secondsInt As Int, errorsInt As Int)
   
    Dim list1 As List
    Dim FullPath As String
    Dim Folder As String ="MathTest"
   
    FullPath=File.Combine(File.DirRootExternal,Folder)
   
    If File.Exists(FullPath,"ListResults.txt") Then 'se esiste

        'carico i dati dal file di testo
        list1=File.ReadList(FullPath, "ListResults.txt")
       
        Dim trovato As Boolean
        trovato = False
       
       
       
            For i = 0 To 99
                listResult(i)=list1.Get(i)
               
                If listResult(i) = "0" And trovato = False Then
                   
                    'memorizzo i dati da registrare
                    listResult(i) = dayTodayString & "|" & monthTodayString & "|" & yearTodayString & "|" & minutesInt & "|" & secondsInt & "|" & errorsInt
                    trovato=True
                       
                End If
               
            Next
           
            'se trova tutti gli slot occupati cancella il primo e sposta tutto di uno
            If trovato=False Then
               
                For i=1 To 99
                    listResult(i-1) = listResult(i)
                Next
               
                listResult(99) = dayTodayString & "|" & monthTodayString & "|" & yearTodayString & "|" & minutesInt & "|" & secondsInt & "|" & errorsInt
               
            End If
               
           
        'salvo i dati nel file   
        File.WriteList(FullPath,"ListResults.txt", listResult)
       
       
    Else 'non esiste
   
       
        File.MakeDir(File.DirRootExternal,Folder)
        FullPath=File.Combine(File.DirRootExternal,Folder)
       
       

            For i = 0 To 99
                listResult(i)="0"
            Next
            '
            listResult(0)=dayTodayString & "|" & monthTodayString & "|" & yearTodayString & "|" & minutesInt & "|" & secondsInt & "|" & errorsInt
           
            File.WriteList(FullPath,"ListResults.txt", listResult)
       

       
   
    End If
   
   
       
End Sub


Sub listButton_Click
   
    StartActivity(List)
   
End Sub

Sub Activity_KeyPress (KeyCode As Int) As Boolean
   
    If KeyCode = KeyCodes.KEYCODE_BACK Then
       
        If Panel1.Visible=True Then
            Panel1.Visible=False
            Return True
        End If

    End If
    Return False

End Sub




Sub infoButton_Click
    StartActivity(Info)
End Sub
 
Upvote 0
D

Deleted member 103

Guest
ok, and what do you advise me to do to solve the problem? thank you
As Erel already wrote, you should only initialize the timers if FirstTime is true.

B4X:
if FirstTime then
    '1000 = 1 secondo
    timer1.Initialize("timer1", 1000)

    'per reclame interstitial
    timerInter.Initialize("timerInter",5000)
end if
 
Upvote 0
D

Deleted member 103

Guest
and in Sub Activity_Resume nothing?
thanks
B4X:
Sub Activity_Resume
timer1.enabled = true
timerInter.enabled = true
End Sub

Sub Activity_Pause
timer1.enabled = false
timerInter.enabled = false
End Sub
 
Upvote 0

powerino

Active Member
Licensed User
don't works... error :-(


B4X:
ub Activity_Resume
   
   
    timer1.enabled = True
    timerInter.enabled = True
   
   
End Sub

Sub timerInter_Tick
   
    caricaIterstitial
    timerInter.Enabled=False
   
End Sub


Sub caricaIterstitial
   
    If IAd.Ready Then IAd.Show

End Sub

Sub IAD_AdClosed
   
    IAd.LoadAd 'preparo il prossimo messaggio pubblicitario
   
End Sub




Sub Activity_Pause (UserClosed As Boolean)

    timer1.enabled = False
    timerInter.enabled = False

End Sub


java.lang.IllegalStateException: Interval must be larger than 0.
 
Upvote 0

powerino

Active Member
Licensed User
B4X:
#Region  Project Attributes
    #ApplicationLabel: Math Test
    #VersionCode: 2
    #VersionName: 1.1
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: portrait
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: True
    #IncludeTitle: False
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

    Dim timer1 As Timer
    Dim stu As StringUtils
    Dim timerInter As Timer

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 BannerAd As AdView
    Private IAd As InterstitialAd
    
    ' per accedere alla memora usando libreria RUNTIME PERMISSION
    Private rp As RuntimePermissions
    
    Private introLabel As Label
    Private mathLabel As Label
    Private okButton As Button
    Private resultField As EditText
    Private titleImage As ImageView
    Dim myImage As Bitmap
    Dim testLevel As Int
    Dim resultInt As Int
    Private errorsLabel As Label
    Private progressTest As ProgressBar
    Private questionNumbLabel As Label
    Dim indexMax As Int
    Dim errorsTotal As Int
    Private backgroundImage As ImageView   
    Private timeLabel As Label
    Dim timePassed As Int
    
    Dim minutes As Int
    Private Panel1 As Panel
    Private panelTitleLAbel As Label
    Private panelLAbel As Label
    Private panelButton As Button
    Dim dayTodayString As String
    Dim monthTodayString As String
    Dim yearTodayString As String
    
    Dim listResult(100) As String
    
    Private listButton As Button
    
    Private dateLabel As Label
    Dim monthString As String
    
    Private infoButton 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("Main")
    
    
    Dim Folder As String ="MathTest"
    File.MakeDir(File.DirRootExternal,Folder)
    Dim FullPath As String=File.Combine(File.DirRootExternal,Folder)
  
    rp.CheckAndRequest(rp.PERMISSION_WRITE_EXTERNAL_STORAGE)  '
    Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
    If Result = False Then
        MsgboxAsync("No permission to access external storage", "")
        Return
    Else
        Dim lstResults As List
        lstResults.Initialize
        lstResults.Add(1)
        lstResults.Add(2)
        lstResults.Add(3)
        Dim FileName As String= "Results.txt"
        Log(FullPath)
        File.WriteList(FullPath,FileName,lstResults)
    End If
    

    FindDate
    CalculateMonth
    
    dateLabel.Text=dayTodayString & " " & monthString & " " & yearTodayString
    
    Panel1.Visible=False
    
    '1000 = 1 secondo
    timer1.Initialize("timer1", 1000)
    timeLabel.TextColor=Colors.Red
    
    'variabile dei secondi e minuti x timer
    timePassed=0
    minutes=0
    timeLabel.Text= "Tempo: 00m:00s"
            
    'setto immagine background
    backgroundImage.Bitmap=LoadBitmap(File.DirAssets, "mathPaper.png")
    
    errorsTotal=0
    errorsLabel.Text="Errori: " & errorsTotal
    
    'imposto la progressbar
    progressTest.Progress=0
    indexMax = 10
    
    'assegno il valore massimo della progressbar
    setMaxProgressBar
    
    'cambio il colore della label
    resultField.Color = Colors.Blue
    resultField.TextColor= Colors.White
    resultField.Hint="Inserisci la tua risposta"
    resultField.HintColor=Colors.Gray
    
    introLabel.TextColor= Colors.Red
    mathLabel.TextColor= Colors.Red
    errorsLabel.TextColor= Colors.Blue
    questionNumbLabel.TextColor= Colors.Blue
    
    
    'spessore progressbar
    Dim gd As GradientDrawable
    gd.Initialize("LEFT_RIGHT", Array As Int(Colors.RGB(211,223,233), Colors.RGB(58,153,233)))
    gd.CornerRadius=3dip
    SetProgressDrawable(progressTest,gd)
        
    'imposto il livello 0 del test
    testLevel=0
    questionNumbLabel.Text = "Livello numero: " & testLevel
    'okButton.Text="OK"
    SetButtonTextSize(okButton,"OK")
    SetButtonTextSize(panelButton,"Chiudi")
    
    
    'setto il titolo con la sua immagine
    titleImage.Bitmap=LoadBitmap(File.DirAssets, "title.png")
    Activity.Title = "Math Test"
    
    
    'info per utente
    'introLabel.Text = "Clicca ok per cominciare"
    SetLabelSize(introLabel,"Clicca ok per cominciare")
    
    'setto il pulsante con la sua immagine
    myImage.Initialize(File.DirAssets, "bottone-blu.png")
    okButton.SetBackgroundImage(myImage)
    panelButton.SetBackgroundImage(myImage)
    
    myImage.Initialize(File.DirAssets, "infoButton.png")
    infoButton.SetBackgroundImage(myImage)
    
    myImage.Initialize(File.DirAssets, "list.png")
    listButton.SetBackgroundImage(myImage)
    
    'setto lo sfondo del panel
    myImage.Initialize(File.DirAssets, "panelBack.png")
    Panel1.SetBackgroundImage(myImage)
    
    panelTitleLAbel.TextColor=Colors.Red
    panelLAbel.TextColor=Colors.Blue
    
    'lancio il banner
    CaricaBanner
    
    'per reclame interstitial
    If FirstTime Then
        'per reclame interstitial
        timerInter.Initialize("timerInter", 5000)
    End If
    
    timerInter.Enabled=True   
    
End Sub

Sub Activity_Resume
    
    
    timer1.enabled = True
    timerInter.enabled = True
    
    
End Sub

Sub timerInter_Tick
    
    caricaIterstitial
    timerInter.Enabled=False
    
End Sub


Sub caricaIterstitial
    
    If IAd.Ready Then IAd.Show

End Sub

Sub IAD_AdClosed
    
    IAd.LoadAd 'preparo il prossimo messaggio pubblicitario
    
End Sub




Sub Activity_Pause (UserClosed As Boolean)

    timer1.enabled = False
    timerInter.enabled = False

End Sub



Sub CaricaBanner
    
    BannerAd.Initialize2("BannerAd", "ca-app-pub-3940256099942544/6300978111", BannerAd.SIZE_SMART_BANNER) 'test banner
    'BannerAd.Initialize2("BannerAd", "ca-app-pub-7754341356106686/9095773896", BannerAd.SIZE_SMART_BANNER)
    Dim height As Int
    If GetDeviceLayoutValues.ApproximateScreenSize < 6 Then
        'phones
        If 100%x > 100%y Then height = 32dip Else height = 50dip
    Else
        'tablets
        height = 90dip
    End If
    Activity.AddView(BannerAd, 0dip, 100%y - height, 100%x, height)
    BannerAd.LoadAd
  
  
    IAd.Initialize("iad", "ca-app-pub-3940256099942544/1033173712")'test interstitial
    'IAd.Initialize("iad", "ca-app-pub-7754341356106686/1100490535")
    IAd.LoadAd
    

    
End Sub

Sub timer1_Tick
    Dim minuteString As String
    Dim secondString As String
    
    
    timePassed=timePassed +1
    If timePassed = 60 Then
        
        minutes = minutes+1
        timePassed=0
                
    End If
    
    If minutes <10 Then
        minuteString= "0" & minutes
    Else
        minuteString= minutes
    End If
    
    If timePassed <10 Then
        secondString= "0" & timePassed
    Else
        secondString= timePassed
    End If
    
    timeLabel.Text= "Tempo: " & minuteString & "m:" & secondString & "s"
        
End Sub


Sub CalculateMonth
    
    If monthTodayString = 1 Then
        monthString="Gennaio"
    else if monthTodayString = 2 Then
        monthString="Febbrai"
    else if monthTodayString = 3 Then
        monthString="Marzo"
    else if monthTodayString = 4 Then
        monthString="Aprile"
    else if monthTodayString = 5 Then
        monthString="Maggio"
    else if monthTodayString = 6 Then
        monthString="Giugno"
    else if monthTodayString = 7 Then
        monthString="Luglio"
    else if monthTodayString = 8 Then
        monthString="Agosto"
    else if monthTodayString = 9 Then
        monthString="Settembre"
    else if monthTodayString = 10 Then
        monthString="Ottobre"
    else if monthTodayString = 11 Then
        monthString="Novembre"
    else if monthTodayString = 12 Then
        monthString="Dicembre"
    End If
    
End Sub


Sub SetProgressDrawable(p As ProgressBar, drawable As Object)
    Dim r As Reflector
    Dim clipDrawable As Object
    clipDrawable = r.CreateObject2("android.graphics.drawable.ClipDrawable", _
      Array As Object(drawable, Gravity.LEFT, 1), _
      Array As String("android.graphics.drawable.Drawable", "java.lang.int", "java.lang.int"))
    r.Target = p
    r.Target = r.RunMethod("getProgressDrawable") 'Gets the layerDrawable
    r.RunMethod4("setDrawableByLayerId", _
      Array As Object(r.GetStaticField("android.R$id", "progress"), clipDrawable), _
      Array As String("java.lang.int", "android.graphics.drawable.Drawable"))
End Sub


Sub setMaxProgressBar ' imposto il max progressbar
    
    Dim rflBar As Reflector
    
    rflBar.Target = progressTest
    rflBar.RunMethod2("setMax", indexMax, "java.lang.int")
    
End Sub


Sub createCalculation
    
    Dim num1 As Int
    Dim num2 As Int
    Dim num3 As Int
    Dim num4 As Int
    
    
    If testLevel = 1 Then
        num1=Rnd(1,101)
        num2=Rnd(1,101)
        'mathLabel.Text = num1 & " + " & num2
        SetLabelSize(mathLabel, num1 & " + " & num2)
        resultInt = num1+num2
    
    Else If testLevel = 2 Then
        num1=Rnd(1,101)
        num2=Rnd(1,101)
        num3=Rnd(1,101)
        'mathLabel.Text = num1 & " + " & num2 & " - " & num3
        SetLabelSize(mathLabel, num1 & " + " & num2 & " - " & num3)
        resultInt = num1+num2-num3
    
    Else If testLevel = 3 Then
        num1=Rnd(1,101)
        num2=Rnd(1,101)
        num3=Rnd(1,101)
        'mathLabel.Text = num1 & " + " & num2 & " - " & num3
        SetLabelSize(mathLabel, "-"& num1 & " + " & num2 & " - " & num3)
        resultInt = -num1+num2-num3
        
    Else If testLevel = 4 Then
        num1=Rnd(1,10)
        num2=Rnd(1,10)
        num3=Rnd(1,101)
        'mathLabel.Text = "(" & num1 & " X " & num2 & ") - " & num3
        SetLabelSize(mathLabel, "(" & num1 & " X " & num2 & ") - " & num3)
        resultInt = num1*num2-num3
        
    Else If testLevel = 5 Then
        num1=Rnd(1,10)
        num2=Rnd(1,10)
        num3=Rnd(1,101)
        'mathLabel.Text = "(" & num1 & " X " & num2 & ") - " & num3
        SetLabelSize(mathLabel, "(" & num1 & " X " & num3 & ") - " & num2 &" + " & num1)
        resultInt = num1*num3-num2+num1
    
    Else If testLevel = 6 Then
        num1=Rnd(1,10)
        num2=Rnd(1,10)
        num3=Rnd(1,101)
        'mathLabel.Text = "((" & num1 & " X " & num2 & ") X " & num1 & ") + " & num3
        SetLabelSize(mathLabel, "((" & num1 & " X " & num2 & ") X " & num1 & ") + " & num3)
        resultInt = num1*num2*num1+num3

    Else If testLevel = 7 Then
        num1=Rnd(1,10)
        num2=Rnd(1,5)
        num3=Rnd(1,101)
        'mathLabel.Text = "((" & num1 & " X " & num2 & ") X " & num1 & ") + " & num3
        SetLabelSize(mathLabel, "(" & num1 & " X " & num2 & " X " & num1 & ") - " & num3 & " + " & num1)
        resultInt = num1*num2*num1-num3+num1
        
    Else If testLevel = 8 Then
        num1=Rnd(1,10)
        num2=Rnd(1,10)
        num3=Rnd(1,10)
        num4=Rnd(1,101)
        'mathLabel.Text = "((" & num1 & " X " & num2 & ") - (" & num1 & " X " & num3 & ")) + " & num4
        SetLabelSize(mathLabel, "((" & num1 & " X " & num2 & ") - (" & num1 & " X " & num3 & ")) + " & num4 )
        resultInt = num1*num2-num1*num3+num4
    
    Else If testLevel = 9 Then
        num1=Rnd(1,9)
        num2=Rnd(1,3)
        num3=Rnd(1,4)
        num4=Rnd(1,151)
        'mathLabel.Text = "((" & num1 & " ^ " & num2 & ") X " & num3 & ") - " & num4
        SetLabelSize(mathLabel, "((" & num1 & " ^ " & num2 & ") X " & num3 & ") - " & num4 )
        resultInt = Power(num1,num2)*num3-num4
    
    Else If testLevel = 10 Then
        num1=Rnd(1,12)
        num2=Rnd(1,4)
        num3=Rnd(1,5)
        num4=Rnd(1,201)
        'mathLabel.Text = "((" & num1 & " ^ " & num2 & ") X " & num3 & ") - " & num4 & " - " & num1
        SetLabelSize(mathLabel, "((" & num1 & " ^ " & num2 & ") X " & num3 & ") - " & num4 & " - " & num1 )
        resultInt = Power(num1,num2)*num3-num4-num1
        
        
    End If
    

    Log(resultInt)
    
    
End Sub

Sub okButton_Click
    
    Dim minuteString As String
    Dim secondString As String
    Dim erroreString As String
        
    If testLevel=0 Then
        
        testLevel=1
        questionNumbLabel.Text = "Livello numero: " & testLevel
        createCalculation
        timer1.Enabled=True 'faccio partire il timer
        'introLabel.Text = "Scrivi il risultato nella finestra blue e clicca OK"
        SetButtonTextSize(okButton,"OK")
        SetLabelSize(introLabel,"Scrivi il risultato nella finestra blue e clicca OK")
    
    Else
        
        'se è giusta
        If resultInt = resultField.Text Then
            
            'pulisco la edit text
            resultField.Text=""
            
            ToastMessageShow("risposta corretta!", True)
            testLevel = testLevel + 1
            questionNumbLabel.Text = "Livello numero: " & testLevel
            progressTest.Progress=progressTest.Progress + 1
            
            If testLevel < (indexMax+1) Then
                createCalculation
            Else
                
                If minutes=1 Then
                    minuteString = " minuto"       
                Else
                    minuteString = " minuti"
                End If
                
                If minutes=1 Then
                    secondString = " secondo"
                Else
                    secondString = " secondi"
                End If
                
                If errorsTotal=1 Then
                    erroreString = " errore"
                Else
                    erroreString = " errori"
                End If
                
                SetLabelSize( mathLabel,"")
                resultField.Text=""
                questionNumbLabel.Text = "Livello numero: "
                'okButton.Text="Nuovo Test"
                SetButtonTextSize(okButton,"Nuovo Test")
                
                okButton.Visible=False
                
                If errorsTotal=0 Then
                    
                    SetLabelSize(panelTitleLAbel, "Complimenti!")
                    SetLabelSize(panelLAbel, "Il test è terminato con " & indexMax & " risposte esatte senza commettere errori in "  & minutes & minuteString & " e " & timePassed & secondString & ". Sei bravissimo!")
                
                else if errorsTotal<3 Then
                
                    SetLabelSize(panelTitleLAbel, "Ben fatto")
                    SetLabelSize(panelLAbel, "Il test è terminato con " & indexMax & " risposte esatte e  " & errorsTotal & erroreString & " in " & minutes & minuteString & " e " & timePassed & secondString & ". Puoi fare di meglio!")
            
                Else
                    SetLabelSize(panelTitleLAbel, "Peccato!")
                    SetLabelSize(panelLAbel, "Il test è terminato con " & indexMax & " risposte esatte e  " & errorsTotal & erroreString & " (troppi) in " & minutes & minuteString & " e " & timePassed & secondString & ". Devi studiare di più!")
                        
                End If
                
                'egisterResults(minutes, timePassed, errorsTotal)
                RegisterResults(minutes, timePassed, errorsTotal)
                
                Panel1.Visible=True
                
                'Msgbox("Il test è terminato con " & indexMax & " risposte esatte e  " & errorsTotal & erroreString & " in " & minutes & minuteString & " e " & timePassed & secondString, "Complimenti")
                
                
                timer1.Enabled=False
                testLevel=0
                progressTest.Progress=0
                errorsTotal=0
                errorsLabel.Text="Errori: " & errorsTotal
                timePassed=0
                minutes=0
                timeLabel.Text= "Tempo: 00m:00s"
                'introLabel.Text = "Clicca ""Nuovo Test"" per cominciare"
                SetLabelSize(introLabel,"Clicca ""Nuovo Test"" per cominciare")
            
            End If
                    
        Else
            
            errorsTotal = errorsTotal +1
            errorsLabel.Text="Errori: " & errorsTotal
            If errorsTotal=1 Then
                erroreString = " errore"
            Else
                erroreString = " errori"
            End If
            Msgbox("La risposta " & testLevel & " è errata! Hai accumulato un totale di " & errorsTotal & erroreString,"Attenzione!")
            
        End If
    
    End If
    
End Sub


'questo serve a modificare la grandezza del testo nella label
Sub SetLabelSize(lbl As Label, txt As String)
    Dim dt As Float
    Dim limit = 0.5 As Float
    Dim h As Int
  
    lbl.Text = txt
    lbl.TextSize = 72
    dt = lbl.TextSize
    h = stu.MeasureMultilineTextHeight(lbl, txt)
    Do While dt > limit Or h > lbl.Height
        dt = dt / 2
        h = stu.MeasureMultilineTextHeight(lbl, txt)
        If h > lbl.Height Then
            lbl.TextSize = lbl.TextSize - dt
            lbl.TextSize = lbl.TextSize - 1 'ho aggiunto questo meno 1 se no a volte la scritta era grande e non si leggeva
        Else
            lbl.TextSize = lbl.TextSize + dt
            lbl.TextSize = lbl.TextSize - 1 'ho aggiunto questo meno 1 se no a volte la scritta era grande e non si leggeva
        End If
    Loop
End Sub
    

'questo serve a modificare la grandezza del testo nel bottone
Sub SetButtonTextSize(lbl As Button, txt As String)
    Dim dt As Float
    Dim limit = 0.5 As Float
    ' Dim limit = 1 As Float
    Dim h As Int
  
    lbl.Text = txt
    lbl.TextSize = 72
    dt = lbl.TextSize
    h = stu.MeasureMultilineTextHeight(lbl, txt)
    Do While dt > limit Or h > lbl.Height
        dt = dt / 2
        h = stu.MeasureMultilineTextHeight(lbl, txt)
        If h > lbl.Height Then
            lbl.TextSize = lbl.TextSize - dt
            lbl.TextSize = lbl.TextSize - 2 'ho aggiunto questo meno 2 se no era grande e andava fuori dal bottone
        Else
            lbl.TextSize = lbl.TextSize + dt
            lbl.TextSize = lbl.TextSize - 2 'ho aggiunto questo meno 2 se no era grande e andava fuori dal bottone
        End If
    Loop
End Sub



Sub panelButton_Click
    Panel1.Visible=False
    okButton.Visible=True
End Sub


Sub FindDate
    
    Dim year As Int = DateTime.GetYear(DateTime.Now)
    Dim month As Int = DateTime.GetMonth(DateTime.Now)
    Dim day As Int = DateTime.GetDayOfMonth(DateTime.Now)
    'Dim hour As Int = DateTime.GetHour(DateTime.bow)
    
    
    
    monthTodayString=month
    dayTodayString=day
    yearTodayString=year
    
    'Log(dayTodayString  & "/" & monthTodayString & "/" & yearTodayString)
    
End Sub



Sub RegisterResults (minutesInt As Int, secondsInt As Int, errorsInt As Int)
    
    Dim list1 As List
    Dim FullPath As String
    Dim Folder As String ="MathTest"
    
    FullPath=File.Combine(File.DirRootExternal,Folder)
    
    If File.Exists(FullPath,"ListResults.txt") Then 'se esiste

        'carico i dati dal file di testo
        list1=File.ReadList(FullPath, "ListResults.txt")
        
        Dim trovato As Boolean
        trovato = False
        
        
        
            For i = 0 To 99
                listResult(i)=list1.Get(i)
                
                If listResult(i) = "0" And trovato = False Then
                    
                    'memorizzo i dati da registrare
                    listResult(i) = dayTodayString & "|" & monthTodayString & "|" & yearTodayString & "|" & minutesInt & "|" & secondsInt & "|" & errorsInt
                    trovato=True
                        
                End If
                
            Next
            
            'se trova tutti gli slot occupati cancella il primo e sposta tutto di uno
            If trovato=False Then
                
                For i=1 To 99
                    listResult(i-1) = listResult(i)
                Next
                
                listResult(99) = dayTodayString & "|" & monthTodayString & "|" & yearTodayString & "|" & minutesInt & "|" & secondsInt & "|" & errorsInt
                
            End If
                
            
        'salvo i dati nel file   
        File.WriteList(FullPath,"ListResults.txt", listResult)
        
        
    Else 'non esiste
    
        
        File.MakeDir(File.DirRootExternal,Folder)
         FullPath=File.Combine(File.DirRootExternal,Folder)
      
        

            For i = 0 To 99
                listResult(i)="0"
            Next
            '
            listResult(0)=dayTodayString & "|" & monthTodayString & "|" & yearTodayString & "|" & minutesInt & "|" & secondsInt & "|" & errorsInt
            
            File.WriteList(FullPath,"ListResults.txt", listResult)
        

        
    
    End If
    
    
        
End Sub


Sub listButton_Click
    
    StartActivity(List)
    
End Sub

Sub Activity_KeyPress (KeyCode As Int) As Boolean
    
    If KeyCode = KeyCodes.KEYCODE_BACK Then
        
        If Panel1.Visible=True Then
            Panel1.Visible=False
            Return True
        End If

    End If
    Return False

End Sub




Sub infoButton_Click
    StartActivity(Info)
End Sub
 
Upvote 0

npsonic

Active Member
Licensed User
You are still initializing one of your timer multiple times.

Try to initialize both timers only ones
B4X:
If FirstTime Then
        timer1.Initialize("timer1", 1000)
        'per reclame interstitial
        timerInter.Initialize("timerInter", 5000)
End If
 
Upvote 0
D

Deleted member 103

Guest
You can upload your project, or better a small project that demonstrates the problem.
You should not post your code but upload your project as a zip file.
b4a.png
 
Upvote 0
Top