Android Question on this code there is a mistake?

khosrwb

Active Member
Licensed User
Longtime User
this code is mistake ?

this error occured when I add the LABEL1 to project
I confused
 

Attachments

  • a1.zip
    34.1 KB · Views: 143

RandomCoder

Well-Known Member
Licensed User
Longtime User
In your code you get all views and assume that they will be imageviews. But after adding the label you no longer have just imageviews and so your program fails because sender is returning a label and you try to cast it to an imageview. You need to first check if the view returned by sender is an imageview and then use it, something like this....
B4X:
Sub imageview_Click
'    im = Sender
'    anim.InitializeAlpha("animAlpha" , 1 , 0.7 )
'    anim.Duration = 600
'    anim.RepeatCount = -1
'    anim.Start(im)

For Each v As View In Activity.GetAllViewsRecursive
        If v Is ImageView Then
            Dim iv As ImageView
            iv = Sender
            anim.InitializeAlpha("animAlpha" , 1 , 0.7 )
            anim.Duration = 600
            anim.RepeatCount = -1
            anim.Start(iv)
            Log("Imageveiw " & iv.Tag)
        Else
            anim.Stop(iv)
        End If
    Next

End Sub

I've added a log statement and you can see that it runs through your loop four times (once for each imageview). If you're wanting to only run through the loop once for the imageview that was clicked then you will need to check the tag value first.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
I would do it that way:
B4X:
Sub Globals
   Private LastImv As ImageView
   Private Label1 As Label
   Private anim As Animation
End Sub

Sub Activity_Create(FirstTime As Boolean)
   For i = 1 To 4
     Private imv As ImageView
     imv.Initialize("ImageViews")
     imv.Tag = i+1   
     imv.Bitmap = LoadBitmap(File.DirAssets,"5.png")
     Activity.AddView(imv,i*20%x ,2%y,18%x,14%y)     
   Next
   Label1.Initialize("Label1")
   Activity.AddView(Label1, 20%x, 20%y, 60%x, 20%y)
   Label1.Color = Colors.White
   Label1.TextColor = Colors.Black
   Label1.TextSize = 22
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub ImageViews_Click
   If LastImv.IsInitialized Then
     anim.Stop(LastImv)
   End If
   
   LastImv = Sender
   anim.InitializeAlpha("animAlpha" , 1 , 0.7 )
   anim.Duration = 600
   anim.RepeatCount = -1
   anim.Start(LastImv)
   Label1.Text = "yes" & " - "& LastImv.Tag
   Label1.Color = Colors.Green
End Sub
 
Upvote 0
Top