Android Question disable button with for next

powerino

Active Member
Licensed User
Hi, I have this problem:

Code: Copy
Sub Globals
...
...
'creo un array di  button
    Private buttons() As Button
    buttons = Array As Button (Button1, Button2, Button3, etc)

and this sub:

Code: Copy
Sub InizializzaBottoni
  
    For i=0 To 2
       
        buttons(i).Initialize("")
        buttons(i).Enabled=False
    
    Next

I have NOT errors, but button are enabled (true)!
 
Hi, I Have added these buttons with the designer, and later write the code.

Code: Copy
Sub Globals
...
...
'creo un array di  button
    Private buttons() As Button
    buttons = Array As Button (Button1, Button2, Button3, etc)

and this sub:

[code]
Sub InizializzaBottoni
  
    For i=0 To 2
       
        buttons(i).Initialize("")
        buttons(i).Enabled=False
    
    Next
I have NOT errors, but button are enabled (true)!
 
Upvote 0
Code: Copy
For i = 0 To Activity.NumberOfViews - 1
        If Activity.GetView(i) Is Button Then
            Activity.GetView(i).Enabled = False
        End If
    Next
 
Upvote 0
Code: Copy
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.


    Dim myImage As Bitmap
    Private sfondo As ImageView
    Private logo As ImageView
            
    Private Button1 As Button
    Private Button2 As Button
    Private Button3 As Button
    Private Button4 As Button
    Private Button5 As Button
        
    
    'creo un array di  button
    Private buttons() As Button
    buttons = Array As Button (Button1, Button2, Button3, Button4, Button5)
 
Upvote 0
Code: Copy
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.


    Dim myImage As Bitmap
    Private sfondo As ImageView
    Private logo As ImageView
           
    Private Button1 As Button
    Private Button2 As Button
    Private Button3 As Button
    Private Button4 As Button
    Private Button5 As Button
       
   
    'creo un array di  button
    Private buttons() As Button
    buttons = Array As Button (Button1, Button2, Button3, Button4, Button5)

I have this error:

java.lang.RuntimeException: Object should first be initialized (Button).
 
Upvote 0
I have this error:

java.lang.RuntimeException: Object should first be initialized (Button).

did you load the layout before disabling the buttons?


Code: Copy
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

    Private Button1 As Button
    Private Button2 As Button
    Private Button3 As Button
    Private Button4 As Button
    Private Button5 As Button
     
 
    'creo un array di  button
    Private buttons() As Button
    buttons = Array As Button (Button1, Button2, Button3, Button4, Button5)

End Sub


Sub Activity_Create(FirstTime As Boolean)
  
    Activity.LoadLayout("nameofyourlayout")

   disable_button

End Sub

Sub disable_button
for i = 0 to buttons.length - 1
buttons(i).enabled = false
next

End Sub
 
Upvote 0
I have the SAME error!!

*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
Error occurred on line: 197 (Main)
java.lang.RuntimeException: Object should first be initialized (Button).
at anywheresoftware.b4a.AbsObjectWrapper.getObject(AbsObjectWrapper.java:50)
at anywheresoftware.b4a.objects.ViewWrapper.setEnabled(ViewWrapper.java:278)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:735)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:357)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:260)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
at com.guerrinosantoni.ColorMatchFull.main.afterFirstLayout(main.java:104)
at com.guerrinosantoni.ColorMatchFull.main.access$000(main.java:17)
at com.guerrinosantoni.ColorMatchFull.main$WaitForLayout.run(main.java:82)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6125)
 
Upvote 0
try this if you just want to disable all the button in an activity, if there's still an error then better upload a small project that shows the problem

Code: Copy
For i = 0 To Activity.NumberOfViews - 1
        If Activity.GetView(i) Is Button Then
            Activity.GetView(i).Enabled = False
        End If
    Next
 
Upvote 0
this is ok, works! but if I need to "select" a particulary name button, i can't make it... for example:

Code: Copy
Sub buttoOn(x As Byte)
    
    For i = 0 To x - 1
        If Activity.GetView(i) Is Button Then
            Activity.GetView(i).Enabled = True
            Log(Activity.GetView(i))
        End If
    Next

    
End Sub

Sub AccendiPulsanti
    
    
    Select levelFile
            
        Case 1
            'Button1.Enabled=True
            buttoOn(1)

Button1 are disabled and NOT enabled
 
Upvote 0
use tag property


Code: Copy
For i = 0 To Activity.NumberOfViews - 1
        If Activity.GetView(i) Is Button Then
            if Activity.GetView(i).Tag = "button1" then
                        ....
            
        End If
    Next
 
Upvote 0
Thanks... now it is ok, but I must write "name button" in TAG property (design) and the correct code is:
Code: Copy
Sub buttoOn(x As Byte)
    
    Dim y As Byte=1
    
    
    For i = 0 To Activity.NumberOfViews - 1
        If Activity.GetView(i) Is Button And x>=y Then
            If Activity.GetView(i).tag = "Button"&y Then
                Log(Activity.GetView(i))
                Activity.GetView(i).Enabled = True
                y=y+1
            End If
        End If
    Next

    
End Sub
 
Upvote 0
This works.
Code: Copy
Sub Globals
    Private Button1, Button2, Button3 As Button
    Private Buttons() As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Main")
    Buttons = Array As Button (Button1, Button2, Button3)
    
    For i = 0 To Buttons.Length - 1
        Buttons(i).Enabled = False
    Next
End Sub
This line
Buttons = Array As Button (Button1, Button2, Button3)
must be after
Activity.LoadLayout("Main")
 

Attachments

Upvote 0
PERFECT! Finally, it is ok! thank you so much...

This line
Buttons = Array As Button (Button1, Button2, Button3)
must be after
Activity.LoadLayout("Main")

See you
 
Upvote 0
Back
Top