I have several iconbuttons and mist find the one with a certain tag. But when I want to work with it, I get
"java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.ViewGroup$LayoutParams com.phillipcalvin.iconbutton.IconButton.getLayoutParams()' on a null object reference"
Why?
B4X:
Dim clickedbtn as IconButton
For Each v As View In Activity.GetAllViewsRecursive
Try
' only check iconbuttons
If v Is IconButton Then
t=v.Tag
Log (t)
If t=clickedno Then
clickedbtn=v
' the next line throws the error
Log (clickedbtn.Left & " - " & clickedbtn.Top)
End If
End If
Catch
Basis.debuginfo (LastException.Message,False)
End Try
Next
v is a VIEW. Views does not have a Tag.
You need to cast v to a IconButton to get the IconButtons Tag.
B4X:
Dim clickedbtn as IconButton
For Each v As View In Activity.GetAllViewsRecursive
Try
' only check iconbuttons
If v Is IconButton Then
dim btn as IconButton = v
t=btn.Tag
Log (t)
If t=clickedno Then
clickedbtn=v
' the next line throws the error
Log (clickedbtn.Left & " - " & clickedbtn.Top)
End If
End If
Catch
Basis.debuginfo (LastException.Message,False)
End Try
Next
Thank you, bit this did not help. The error message remains.
And then, this does not work:
B4X:
Dim bb As BitmapDrawable
bb.Initialize(Basis.CreateScaledBitmap2(LoadBitmap(File.DirAssets,"ic_play_black_48dp.png"), 40dip, 40dip, True))
clickedbtn.setIcon (False,bb)
It throws no error, but it does not work. If I say "clickedbtn is the newest button" - it works, but on the wrong button.
sorry, my code was wrong. maybe this is the problem
B4X:
For Each v As View In Activity.GetAllViewsRecursive
Try
' only check iconbuttons
If v Is IconButton Then
dim btn as IconButton = v
t=btn.Tag
Log (t)
If t=clickedno Then
' the next line throws the error
Log (btn.Left & " - " & btn.Top)
End If
End If
Catch
Basis.debuginfo (LastException.Message,False)
End Try
Next
Is it really? The problem is - I need to find the iconbutton with a certain tag and cast a global variable to it which I can use in another part of the program.
Your code in post #1 does not tell this requirement that there is a gobal variable
Create a global variable
B4X:
Dim clickedbtn as IconButton
and then you can use it like this i guess
B4X:
For Each v As View In Activity.GetAllViewsRecursive
Try
' only check iconbuttons
If v Is IconButton Then
dim btn as IconButton = v
clickedbtn = btn
t=btn.Tag
Log (t)
If t=clickedno Then
' the next line throws the error
Log (btn.Left & " - " & btn.Top)
End If
End If
Catch
Basis.debuginfo (LastException.Message,False)
End Try
Next
If not i guess you should post a project which shows the problem.