Android Question [SOLVED] Switch error

jayel

Active Member
Licensed User
Hello,

When I want to set the switch checked value I get an error :
B4X:
    Dim cv As SwitchView
    cv.Switch = LayoutPanel.GetView(1)
    cv.Checked = True
Error :
itemid = 30
Error occurred on line: 47 (switchview)
java.lang.NullPointerException
at be.robotronic.robodomotica.switchview._setchecked(switchview.java:49)
at be.robotronic.robodomotica.subitems._input_contentfiller(subitems.java:538)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:636)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:302)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:238)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:121)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:175)
at flm.b4a.ultimatelistview.b.getView(SourceFile:561)
at android.widget.AbsListView.obtainView(AbsListView.java:2724)
at android.widget.ListView.makeAndAddView(ListView.java:1811)
at android.widget.ListView.fillDown(ListView.java:697)
at android.widget.ListView.fillFromTop(ListView.java:763)
at android.widget.ListView.layoutChildren(ListView.java:1627)
at flm.b4a.ultimatelistview.UltimateListView.layoutChildren(SourceFile:549)
at android.widget.AbsListView.onLayout(AbsListView.java:2549)
at android.view.View.layout(View.java:15746)
at android.view.ViewGroup.layout(ViewGroup.java:4867)
at anywheresoftware.b4a.BALayout.onLayout(BALayout.java:43)
at android.view.View.layout(View.java:15746)
at android.view.ViewGroup.layout(ViewGroup.java:4867)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
at android.view.View.layout(View.java:15746)
at android.view.ViewGroup.layout(ViewGroup.java:4867)
at com.android.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:459)
at android.view.View.layout(View.java:15746)
at android.view.ViewGroup.layout(ViewGroup.java:4867)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
at android.view.View.layout(View.java:15746)
at android.view.ViewGroup.layout(ViewGroup.java:4867)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2356)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2069)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1254)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6630)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:803)
at android.view.Choreographer.doCallbacks(Choreographer.java:603)
at android.view.Choreographer.doFrame(Choreographer.java:573)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:789)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5479)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)

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

jayel

Active Member
Licensed User
I tought I posted the error?
But here is the error :
Error occurred on line: 47 (switchview)
java.lang.NullPointerException
at be.robotronic.robodomotica.switchview._setchecked(switchview.java:49)
at be.robotronic.robodomotica.subitems._input_contentfiller(subitems.java:538)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:636)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:302)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:238)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:121)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:175)
at flm.b4a.ultimatelistview.b.getView(SourceFile:561)
at android.widget.AbsListView.obtainView(AbsListView.java:2724)
at android.widget.ListView.makeAndAddView(ListView.java:1811)
at android.widget.ListView.fillDown(ListView.java:697)
at android.widget.ListView.fillFromTop(ListView.java:763)
at android.widget.ListView.layoutChildren(ListView.java:1627)
at flm.b4a.ultimatelistview.UltimateListView.layoutChildren(SourceFile:549)
at android.widget.AbsListView.onLayout(AbsListView.java:2549)
at android.view.View.layout(View.java:15746)
at android.view.ViewGroup.layout(ViewGroup.java:4867)
at anywheresoftware.b4a.BALayout.onLayout(BALayout.java:43)
at android.view.View.layout(View.java:15746)
at android.view.ViewGroup.layout(ViewGroup.java:4867)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
at android.view.View.layout(View.java:15746)
at android.view.ViewGroup.layout(ViewGroup.java:4867)
at com.android.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:459)
at android.view.View.layout(View.java:15746)
at android.view.ViewGroup.layout(ViewGroup.java:4867)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
at android.view.View.layout(View.java:15746)
at android.view.ViewGroup.layout(ViewGroup.java:4867)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2356)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2069)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1254)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6630)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:803)
at android.view.Choreographer.doCallbacks(Choreographer.java:603)
at android.view.Choreographer.doFrame(Choreographer.java:573)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:789)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5479)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
 

jayel

Active Member
Licensed User
The error gets hit in the SwitchView class module :
B4X:
Public Sub setChecked(b As Boolean)
   
   
    joSwitch.RunMethod("setChecked", Array As Object(b))
End Sub
 

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....
 

Erel

Administrator
Staff member
Licensed User
Let the designer generate the variable for you.

The correct code is:
B4X:
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
 

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?
B4X:
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
 

jayel

Active Member
Licensed User
But if I want the Switchview of the first one after the seconde one is added?
 

jayel

Active Member
Licensed User
Below doesn't work
B4X:
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 :
** Activity (main) Create, isFirst = true **
Error occurred on line: 41 (main)
java.lang.RuntimeException: Field: ba not found in: android.widget.Switch
at anywheresoftware.b4a.shell.Shell$FieldCache.getField(Shell.java:833)
at anywheresoftware.b4a.shell.Shell.getField(Shell.java:604)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:314)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:238)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:121)
at b4a.testswitchviewerror.main.afterFirstLayout(main.java:100)
at b4a.testswitchviewerror.main.access$100(main.java:17)
at b4a.testswitchviewerror.main$WaitForLayout.run(main.java:78)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5479)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
** Activity (main) Resume **
 

DonManfred

Expert
Licensed User
B4X:
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

Active Member
Licensed User
Any solution is great for me....

Thanks I will try that in conjunction with ultimate listview
 
Top