Serious problem with a Button

vinians

Member
Licensed User
Longtime User
I'm creating a series of buttons in run time. The code is below:
B4X:
Sub HTTP_ResponseSuccess (Response As HttpResponse, TaskId As Int)
    Dim esq    As Boolean
   Dim w, h   As Int
   Dim sep    As Int
   Log("ResponseSuccess")
   ProgressDialogHide()
   Log("1")
   If (Funcoes.PreparaResposta(Response.GetString("UTF8")) = "OK") Then
      Log("2")
      esq  = True
      sep  = 10dip
      w    = (90%x / 2)  
      h    = 10%y
      For i = 0 To Funcoes.g_resposta.Size - 1
         Log("3")
         Dim b As Button
         b.Initialize("btnAgrupamento")
         Log("4")
         If (esq = True) Then 
            b.Left = 5dip
         Else
            b.Left = w + sep + sep
         End If
         Log("6")
         b.Height = h
         b.Width  = w
         Log("7")
         b.Text   = Funcoes.g_resposta.Get(i)
         Log("8")
         If (esq) Then 
            esq = False 
         Else 
            esq = True
         End If
         Log("9")
      Next         
   Else
      Funcoes.Erro(Funcoes.g_ultimo_erro)   
      Activity.Finish()
   End If
End Sub
Sub btnAgrupamento_Click
   
End Sub
I inserted a lot of "logs" because I wanted to know the exact location of the problem. The error seems to be in the line b.left = 5dip. The log is the follow:
** Activity (main) Pause, UserClosed = false **
** Activity (selecionagarcon) Create, isFirst = true **
** Activity (selecionagarcon) Resume **
ResponseSuccess
** Activity (selecionagarcon) Pause, UserClosed = false **
** Activity (selecionacomanda) Create, isFirst = true **
** Activity (selecionacomanda) Resume **
** Activity (selecionacomanda) Pause, UserClosed = false **
** Activity (selecionaagrupamento) Create, isFirst = true **
** Activity (selecionaagrupamento) Resume **
ResponseSuccess
1
2
3
4
selecionaagrupamento_http_responsesuccess (java line: 333)
java.lang.NullPointerException
at anywheresoftware.b4a.objects.ViewWrapper.setLeft(ViewWrapper.java:139)
at xcreator.com.selecionaagrupamento._http_responsesuccess(selecionaagrupamento.java:333)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:165)
at anywheresoftware.b4a.BA$3.run(BA.java:301)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
java.lang.NullPointerException
Please, I cannot figure out why this error is happening. Any help?
 

klaus

Expert
Licensed User
Longtime User
You must add your Buttons somewhere either on the Activity or a Panel with:
B4X:
Dim l, t, w, h   As Int
'
'
'
w = 45%x  
h = 10%y
If (esq = True) Then 
  l = 5dip
  t = ?
Else
  l = w + sep + sep
  t = ?
End If
Activity.AddView(btnAgrupamento, l, t, w, h)
btnAgrupamento.Tag = i
You must also define the Top property and set the Tag property to identify the buttons in the btnAgrupamento_Click routine.
Before you have added a view you cannot access the Left, Top, Width nor Height properties.

You need to use the Sender keyword to know what button raised the event.
B4X:
Sub btnAgrupamento_Click
    Dim btn As Button
    Dim ButtonI As Int    ' index of the button that raised the event
    btn = Sender
    
    ButtonI = btn.Tag
End Sub
Best regards.
 
Upvote 0

vinians

Member
Licensed User
Longtime User
You must add your Buttons somewhere either on the Activity or a Panel with:
hhmm, I do not knew that was necessary to use Activity.AddView() before setting positioning properties!
About the use of "tag" I dont need because I will use "text" and "sender" to identify what button was clicked. Thank you a lot!

I will try, thanks!
 
Upvote 0
Top