Android Question [SOLVED] Switch error

Discussion in 'Android Questions' started by jayel, Jun 2, 2015.

  1. jayel

    jayel Active Member Licensed User

    Hello,

    When I want to set the switch checked value I get an error :
    Code:
    Dim cv As SwitchView
        cv.Switch = LayoutPanel.GetView(
    1)
        cv.Checked = 
    True
    Error :

    This is the switch I am using : http://www.b4x.com/android/forum/threads/switch-view-android-4.38971/#content
     
  2. klaus

    klaus Expert Licensed User

    And what error do you get ?
     
  3. jayel

    jayel Active Member Licensed User

    I tought I posted the error?
    But here is the error :
     
  4. jayel

    jayel Active Member Licensed User

    The error gets hit in the SwitchView class module :
    Code:
    Public Sub setChecked(b As Boolean)
       
       
        joSwitch.RunMethod(
    "setChecked"Array As Object(b))
    End Sub
     
  5. klaus

    klaus Expert Licensed User

    The error reports a NullPointerExeption.
    Are you sure that LayoutPanel.GetView(1) is a SwitchView ?
     
  6. jayel

    jayel Active Member Licensed User

    I have changed the layout and I have 1 customview (SwitchView). So no other views
    It throws the same error....
     
  7. klaus

    klaus Expert Licensed User

    Could you post a small project showing the problem.
     
    Peter Simpson likes this.
  8. jayel

    jayel Active Member Licensed User

    In attachment a small project with the same error.
     

    Attached Files:

  9. Erel

    Erel Administrator Staff Member Licensed User

    Let the designer generate the variable for you.

    The correct code is:
    Code:
    Sub Globals
       
    Private CustomView1 As SwitchView
    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")
       Dim panel1 As Panel
       panel1.Initialize(
    "panel1")
       
       
    Activity.AddView(panel1,10dip,10dip,100%x,100%y)
       panel1.LoadLayout(
    "switchlayout")
       CustomView1.Checked = 
    True
    End Sub
     
    Peter Simpson likes this.
  10. jayel

    jayel Active Member Licensed User

    Indeed this works, but I use it in a listitem (ultimate listview).
    So If a add 2 panels with each of then the layout of the customview, how do I access the switch in each panel?
    Code:
    Sub Globals
        
    'These global variables will be redeclared each time the activity is created.
        'These variables can only be accessed from this module.
       
        
    Private CustomView1 As SwitchView
    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")
        Dim panel1 As Panel
        
    Dim panel2 As Panel
        panel1.Initialize(
    "panel1")
        panel2.Initialize(
    "panel2")
        
    Activity.AddView(panel1,10dip,10dip,100%x,200dip)
        
    Activity.AddView(panel2,10dip,210dip,100%x,200dip)
        panel1.LoadLayout(
    "switchlayout")
        panel2.LoadLayout(
    "switchlayout")
    '    Dim sw As SwitchView
    '    sw.Switch = panel1.GetView(0)
        CustomView1.Checked = True
    End Sub
     
  11. Erel

    Erel Administrator Staff Member Licensed User

    There are several ways to do it. The best one is:
    Code:
    panel1.LoadLayout("switchlayout")
    Dim sv As SwitchView = CustomView1 'CustomView1 will hold a reference to the last SwitchView added.
    sv.Checked = True
     
    Peter Simpson likes this.
  12. jayel

    jayel Active Member Licensed User

    But if I want the Switchview of the first one after the seconde one is added?
     
  13. DonManfred

    DonManfred Expert Licensed User

    You can create a global list of Objects and add the object(s) (CustomView1) to this list.

    list.get(0) is the first, 1 the second....
     
    Peter Simpson likes this.
  14. jayel

    jayel Active Member Licensed User

    Below doesn't work
    Code:
    Sub Globals
        
    'These global variables will be redeclared each time the activity is created.
        'These variables can only be accessed from this module.
       
        
    Private CustomView1 As SwitchView
    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")
        Dim panel1 As Panel
        
    Dim panel2 As Panel
        panel1.Initialize(
    "panel1")
        panel2.Initialize(
    "panel2")
        
    Activity.AddView(panel1,10dip,10dip,100%x,200dip)
        
    Activity.AddView(panel2,10dip,210dip,100%x,200dip)
        panel1.LoadLayout(
    "switchlayout")
        panel2.LoadLayout(
    "switchlayout")
    '    Dim sw As SwitchView
        CustomView1 = panel1.GetView(0)
        CustomView1.Checked = 
    True
    End Sub

    Sub Activity_Resume

    End Sub

    Sub Activity_Pause (UserClosed As Boolean)

    End Sub
    error :
     
  15. DonManfred

    DonManfred Expert Licensed User

    this code has nothing to do with that what i said/suggested!? So you are using another solution here???
     
  16. DonManfred

    DonManfred Expert Licensed User

    Code:
    Sub Globals
        
    'These global variables will be redeclared each time the activity is created.
        'These variables can only be accessed from this module.
        dim objlist as list
        
    Private CustomView1 As SwitchView
    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")
        objlist.initialize    
        
    Dim panel1 As Panel
        
    Dim panel2 As Panel
        panel1.Initialize(
    "panel1")
        panel2.Initialize(
    "panel2")
        
    Activity.AddView(panel1,10dip,10dip,100%x,200dip)
        
    Activity.AddView(panel2,10dip,210dip,100%x,200dip)
        panel1.LoadLayout(
    "switchlayout")
        objlist.add(CustomView1)
        panel2.LoadLayout(
    "switchlayout")
       objlist.add(CustomView1)

        
    Dim sw As SwitchView = objlist.get(0' first
        sw.Checked = True

        
    Dim sw As SwitchView = objlist.get(1' second
        sw.Checked = True

    End Sub
     
    jayel likes this.
  17. jayel

    jayel Active Member Licensed User

    Any solution is great for me....

    Thanks I will try that in conjunction with ultimate listview
     
  18. DonManfred

    DonManfred Expert Licensed User

    should work with the ULV too
     
  19. jayel

    jayel Active Member Licensed User

    It worked with ULV too.... thanks to all, and Manfred !
     
    DonManfred likes this.
  20. DonManfred

    DonManfred Expert Licensed User

    You should not be crispy with clicking the like if someone post a useful answer for you. That´s the only countable honor anyone can get when he helps
     
    Gunther likes this.
Loading...