Android Question Spinner initialization problem

danM

Member
Licensed User
Longtime User
Hello,

I have an interesting problem and it goes as follows:
1) I define a global spinner:
Dim geometricBtn As Spinner
2) I try to initialize the spinner (not included in any designer layout):
geometricBtn.Initialize("geometricBtn")
3) then I get the following error:

LogCat connected to: emulator-5554
** Activity (main) Create, isFirst = true **


java.lang.RuntimeException: Don't call setOnClickListener for an AdapterView. You probably want setOnItemClickListener instead


at android.widget.AdapterView.setOnClickListener(AdapterView.java:773)
at anywheresoftware.b4a.objects.ViewWrapper.innerInitialize(ViewWrapper.java:73)
at anywheresoftware.b4a.objects.SpinnerWrapper.innerInitialize(SpinnerWrapper.java:46)
at anywheresoftware.b4a.objects.ViewWrapper.Initialize(ViewWrapper.java:64)
at malta.aeroCalc.main._activity_create(main.java:486)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:175)
at malta.aeroCalc.main.afterFirstLayout(main.java:98)
at malta.aeroCalc.main.access$100(main.java:16)
at malta.aeroCalc.main$WaitForLayout.run(main.java:76)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
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:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)

Could anyone tell me what might be the problem?

Thanks,
Dan
 

stevel05

Expert
Licensed User
Longtime User
Is it in the main activity or a Class or code module? Can you zip and post your project?
 
Upvote 0

danM

Member
Licensed User
Longtime User
sure thing, I added it below.
The error happens at the line:
"geometricBtn.Initialize("geometricBtn")"

here is the code

Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
End Sub

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

'layout related variables
Dim clearLbl As Label
Dim aboutLbl As Label
Dim exitLbl As Label
Dim mainScroll As ScrollView

'geometric altitude gui
Dim geometricPnl As Panel
Dim geometricLbl As Label
Dim geometricEdit As EditText
Dim geometricBtn As Spinner

'gui constants
Dim panelHeight As Int = 35dip 'scroll view added panels constant measurements
Dim panelVerticalDist As Int = 2dip 'vertical distance between panels
Dim panelScreenDist As Int = 2dip 'horizontal distance between panels and screen borders
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("main")

'local variables
Dim i As Int = 0

'add geometric altitude gui
geometricPnl.Initialize("")
geometricPnl.Color = Colors.rgb(206, 206, 206)
mainScroll.Panel.AddView(geometricPnl, panelScreenDist, i * (panelVerticalDist + panelHeight), 100%x - 2 * panelScreenDist, panelHeight)

geometricLbl.Initialize("geometricLbl")
geometricLbl.TextSize = 15
geometricLbl.TextColor = Colors.black
geometricLbl.Color = Colors.rgb(206, 206, 206)
geometricLbl.Text = "Geometric altitude"
geometricLbl.Gravity = Bit.OR(Gravity.CENTER_VERTICAL, Gravity.LEFT)
geometricPnl.AddView(geometricLbl, 0, 0, 50%x, panelHeight)

geometricEdit.Initialize("geometricEdit")
geometricEdit.TextSize = 15
geometricEdit.TextColor = Colors.black
geometricEdit.Color = Colors.rgb(206, 206, 206)
geometricEdit.Gravity = Gravity.FILL
geometricEdit.Hint = "..."
geometricEdit.HintColor = Colors.DarkGray
geometricEdit.InputType = geometricEdit.INPUT_TYPE_NUMBERS
geometricPnl.AddView(geometricEdit, 50%x, 0, 75%x, panelHeight)

geometricBtn.Initialize("geometricBtn")
geometricBtn.TextSize = 15
geometricBtn.TextColor = Colors.Black
geometricBtn.Color = Colors.rgb(206, 206, 206)
geometricBtn.AddAll(Array As String("m", "ft"))
geometricBtn.DropdownBackgroundColor = Colors.rgb(206, 206, 206)
geometricBtn.DropdownTextColor = Colors.Black
geometricBtn.SelectedIndex = 0
geometricBtn.Prompt = "geometric altitude dimension"
geometricPnl.AddView(geometricBtn, 75%x, 0, 100%x, panelHeight)

i = i + 1

end sub
 
Upvote 0

Peter Simpson

Expert
Licensed User
Longtime User
Hello @danM,
I couldn't find any real issues with your code. I did add mainScroll.Initialize(Activity.Height) to get what you posted working, but what you really have to do is Click on the file menu in B4A, then export as zip and post that zip file onto here.
B4X:
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
  
End Sub

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

'layout related variables
    Dim clearLbl As Label
    Dim aboutLbl As Label
    Dim exitLbl As Label
    Dim mainScroll As ScrollView

'geometric altitude gui
    Dim geometricPnl As Panel
    Dim geometricLbl As Label
    Dim geometricEdit As EditText
    Dim geometricBtn As Spinner

'gui constants
    Dim panelHeight As Int = 35dip 'scroll view added panels constant measurements
    Dim panelVerticalDist As Int = 2dip 'vertical distance between panels
    Dim panelScreenDist As Int = 2dip 'horizontal distance between panels and screen borders
  
'local variables
    Private i As Int = 0  
End Sub

Sub Activity_Create(FirstTime As Boolean)
'Do not forget to load the layout file created with the visual designer. For example:
    mainScroll.Initialize(Activity.Height)
  
    Activity.LoadLayout("main")

'add geometric altitude gui
    geometricPnl.Initialize("")
  
  
    geometricPnl.Color = Colors.rgb(206, 206, 206)
    mainScroll.Panel.AddView(geometricPnl, panelScreenDist, i * (panelVerticalDist + panelHeight), 100%x - 2 * panelScreenDist, panelHeight)

    CallSub(Null, GeogeometricSub)
End Sub

Sub GeogeometricSub
    geometricLbl.Initialize("geometricLbl")
    geometricLbl.TextSize = 15
    geometricLbl.TextColor = Colors.black
    geometricLbl.Color = Colors.rgb(206, 206, 206)
    geometricLbl.Text = "Geometric altitude"
    geometricLbl.Gravity = Bit.OR(Gravity.CENTER_VERTICAL, Gravity.LEFT)
    geometricPnl.AddView(geometricLbl, 0, 0, 50%x, panelHeight)

    geometricEdit.Initialize("geometricEdit")
    geometricEdit.TextSize = 15
    geometricEdit.TextColor = Colors.black
    geometricEdit.Color = Colors.rgb(206, 206, 206)
    geometricEdit.Gravity = Gravity.FILL
    geometricEdit.Hint = "..."
    geometricEdit.HintColor = Colors.DarkGray
    geometricEdit.InputType = geometricEdit.INPUT_TYPE_NUMBERS
    geometricPnl.AddView(geometricEdit, 50%x, 0, 75%x, panelHeight)

    geometricBtn.Initialize("geometricBtn")
    geometricBtn.TextSize = 15
    geometricBtn.TextColor = Colors.Black
    geometricBtn.Color = Colors.rgb(206, 206, 206)
    geometricBtn.AddAll(Array As String("m", "ft"))
    geometricBtn.DropdownBackgroundColor = Colors.rgb(206, 206, 206)
    geometricBtn.DropdownTextColor = Colors.Black
    geometricBtn.SelectedIndex = 0
    geometricBtn.Prompt = "geometric altitude dimension"
    geometricPnl.AddView(geometricBtn, 75%x, 0, 100%x, panelHeight)

    i = i + 1
End Sub
 
Upvote 0

danM

Member
Licensed User
Longtime User
I,
here is my project as a ZIP file.

Thanks,
dan
 

Attachments

  • aero.zip
    10.6 KB · Views: 244
  • aero.zip
    10.6 KB · Views: 257
Upvote 0

danM

Member
Licensed User
Longtime User
Hi,

Thank you for your advice, but it didn't work.
still getting on line "geometricBtn.Initialize("geometricBtnX")"
the error:
"java.lang.RuntimeException: Don't call setOnClickListener for an AdapterView. You probably want setOnItemClickListener instead"
 
Upvote 0

danM

Member
Licensed User
Longtime User
Solved!
Had to change all calls to spinner's from "_Click" to "_ItemClick (Position As Int, Value As Object)"
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
wrongevents.png
still getting on line "geometricBtn.Initialize("geometricBtnX")"
cause you are still defining the wrong event-subs

for example: You declared a "click" event but a spinner does not have a clickevent
B4X:
Sub densityAltitudeBtn_click
End Sub

it should be something like
B4X:
Sub Spinner1_ItemClick (Position As Int, Value As Object)
End Sub
for a spinner.
 

Attachments

  • erofixed.zip
    10.6 KB · Views: 251
Upvote 0

Jiemde

Member
Licensed User
Longtime User
Hi to all, and a Happy New year 2015!

I have some problems with the spinner view!
I had add a spinner function with the designer and it appears correctly in the designer view ( so normally no need to initialize it in the code )
When I run the project it appears correctly but when I click on it ...... nothing!
In the debug windows I see an error who say that it is not initialized! ( but also remark that 90% of the var définition in the sub Global are underlined! an show a message "never assigned a value, or is not initialized "
Can you help me because, except the spinner my project is working well!

Best regards
Jiemde
 
Upvote 0

Jiemde

Member
Licensed User
Longtime User
Hi Manfred, sorry for the wrong thread!
The zip file is too large for uploading it !
Thanks for your help

Jiemde
 
Upvote 0

Jiemde

Member
Licensed User
Longtime User
Hi Klaus,

I tink Yes

Dim CoefArray(11) As String
Dim SpnrTOD, SpnrMOD As Spinner in the Sub Globals

CoefArray(0)="1.0"
CoefArray(1)="1.1"
CoefArray(2)="1.2"
CoefArray(3)="1.3"
CoefArray(4)="1.4"
CoefArray(5)="1.5"
CoefArray(6)="1.6"
CoefArray(7)="1.7"
CoefArray(8)="1.8"
CoefArray(9)="1.9"
CoefArray(10)="2.0"

SpnrTOD.AddAll(CoefArray)
SpnrMOD.AddAll(CoefArray) in the Sub Activity_Create


Sub SpnrTod_ItemClick (Position As Int, Value As Object)

End Sub

Sub SpnrMod_ItemClick (Position As Int, Value As Object)

End Sub

What I see and I think it's not "normal" is that a great part of the Dim definition is always underligned and show problem like "warnings #10 or #11 !
Best regards
Jiemde
 
Upvote 0

Jiemde

Member
Licensed User
Longtime User
Re,

I understand that it's difficult without the code but as I say the "upload a file" work till 512K ! and my zipfile is too large than that, so how to send you the zipfile ? give me an email adress and I will send it

Hope you can help me
Jiemde
 
Upvote 0
Top