Android Question Odd Problem with AddView

andrewj

Active Member
Licensed User
Longtime User
Hi,
Can anyone help me to find out what's going on here, please?

I have some code which is working in several activities throughout my application without any problem, but I have recently added it to another activity (since upgrading to V3.8) and I'm getting an unresolvable error.

In the activity Globals section I declare a ListView object:

B4X:
Dim lstMenu As ListView

Then in the trigger event I have the following code:

B4X:
    Dim vw As View = Sender
    If lstMenu.IsInitialized AND lstMenu.Visible Then lstMenu.Visible = False
    lstMenu.Initialize("lstMenu")
    lstMenu.Color = Colors.DarkGray
    sMenuMode = "View"
    lstMenu.AddSingleLine("Origin")
    lstMenu.AddSingleLine("Scale")
    lstMenu.AddSingleLine("Stretch")
    lstMenu.AddSingleLine("Zoom")
    Activity.AddView(lstMenu, vw.left, oVideo.Top, 200dip, 460dip)
    lstMenu.Visible = True

At the Activity.AddView point, I get the following error:
"java.lang.ClassCastException: java.lang.String cannot be cast to android.view.View"

For some reason this instance of the code seems to not recognise lstMenu as a valid child view, whereas as I said, it works perfectly everywhere else.

Any ideas?
Thanks
Andrew
 

DonManfred

Expert
Licensed User
Longtime User
in which sub you have this code?
Dim vw AsView = Sender will use the object which calls the sub... maybe a edit-text? or a label?

if it is called from clicking a listviewitem then it should be
Dim vw As ListView = Sender
 
Upvote 0

andrewj

Active Member
Licensed User
Longtime User
Hi,
The sender is nothing to do with this code - it's just a button. I only need a reference to it for its position.
Thanks
Andrew
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
There is not really enough information to make it easy to give you an answer, but I suspect that the problem lies in the fact that you are initializing and adding the Listview multiple times.

Try changing the code to
:
B4X:
Dim vw As View = Sender
    If lstMenu.IsInitialized Then
          If lstMenu.Visible Then
               lstMenu.Visible = False
               Return
          Else
               lstMenu.Visible = True
               Return
          End if
    End If
    lstMenu.Initialize("lstMenu")
    lstMenu.Color = Colors.DarkGray
    sMenuMode = "View"
    lstMenu.AddSingleLine("Origin")
    lstMenu.AddSingleLine("Scale")
    lstMenu.AddSingleLine("Stretch")
    lstMenu.AddSingleLine("Zoom")
    Activity.AddView(lstMenu, vw.left, oVideo.Top, 200dip, 460dip)
    lstMenu.Visible = True
 
Upvote 0

andrewj

Active Member
Licensed User
Longtime User
Hi
I don't think that's the problem. I use code identical to this to repeatedly show and hide menus elsewhere in my app and it's fine. Also the problem occurs first time I hit the AddView line in this Activity.

Any other ideas?
Thanks Andrew
 
Upvote 0

Diabolus

Member
Licensed User
Longtime User
Why do you create a new list every time and not use the same?

Why not create it once and use .visible = true/false and .setlayout if you want to change the position?

- - -

Have you tested with left/top = 0dip ?
 
Last edited:
Upvote 0

andrewj

Active Member
Licensed User
Longtime User
Hi,
I've solved the problem - it was actually with referring to vw.Left (to get the position of the icon on the taskbar). I've hard-coded the left position and the code is working fine now.
Thanks for all the suggestions
Andrew
 
Upvote 0
Top