Android Question ScrollView runtime error, please help!

Beja

Expert
Licensed User
Longtime User
Please tell me what's wrong with this code?

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


at anywheresoftware.b4a.AbsObjectWrapper.getObject(AbsObjectWrapper.java:46)
at anywheresoftware.b4a.samples.customlistview.main._chk_checkedchange(main.java:339)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:175)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:163)
at anywheresoftware.b4a.objects.CompoundButtonWrapper$1.onCheckedChanged(CompoundButtonWrapper.java:44)
at android.widget.CompoundButton.setChecked(CompoundButton.java:124)
at android.widget.CompoundButton.toggle(CompoundButton.java:86)
at android.widget.CompoundButton.performClick(CompoundButton.java:98)
at android.view.View$PerformClick.run(View.java:8816)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)
java.lang.RuntimeException: Object should first be initialized (View).
 

Attachments

  • scrollview.zip
    8.7 KB · Views: 220

klaus

Expert
Licensed User
Longtime User
Are you sure that the project you posted is the same as yours ?
Have you tested the project you posted ?
In the title you speak about a ScrollView but in the code it's a CustonListView !!??

First point: in your code you want to load a layout, but there is no layout file in your project !?
Runing the project throughs an error File not found !
Commenting out the line Activity.LoadLayout("1") it works !!??
 
Upvote 0

Beja

Expert
Licensed User
Longtime User
Hi Klaus,
Sorry it is the customlistview example as you noticed.. I am trying to modify it.. I removed the buttons and commented out the loadlayout line.. it didn't work then I put the line back. please see attached screenshot.
 

Attachments

  • errorlog.jpg
    errorlog.jpg
    118.3 KB · Views: 335
Upvote 0

Beja

Expert
Licensed User
Longtime User
3.82

Thanks for your help.. indeed something wrong in my side. I tested on the emulator but will see how it does in real device..
 
Upvote 0

Beja

Expert
Licensed User
Longtime User
3.82
Thanks for your help.. indeed something wrong in my side. I tested on the emulator but will see how it does in real device..
Kind of weird...
 
Upvote 0

Beja

Expert
Licensed User
Longtime User
The strange thing is that, when I say ok and the error message goes away.. the result of the action (check - uncheck) is correctly displayed.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Sorry, I get it too now.
As you hadn't s said that the problem occures when you click a CheckBox I only tested compiling and didn't see it.
The problem is this line :
chk = pnl.GetView(2)
you must relpace 2 by 1 !
chk = pnl.GetView(1)

The original demo program contains 3 views per panel, but you use only 2 !
chk = pnl.GetView(2) looks for the third view which doesn't exist !
 
Upvote 0

Beja

Expert
Licensed User
Longtime User
Hi Klaus,
again a little question.. I didn't know how to give different text (caption) to the buttons and lables.. in fact that's why I tried to get rid of them.. now all displaying the text "Click"
they are not in array otherwise I would have called them with their index.
Sorry for the so many Qs and thanks in advance.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
In the CustomListView demo program we have this line
b.Text = "Click"
in the CreateListItem routine.
You need to adapt this routine to your needs.
What texts do you want to give the buttons, where do they come from ?
You could use an array of strings with the button texts to add them.
 
Upvote 0

Beja

Expert
Licensed User
Longtime User
Thanks Klaus,
The text I want on different buttons are stored in an editable text file (outside the assets) something like:
Water
Soda
Bread
Orange Juice
Apple
Ice cubes
French Fries
Chicken
Fish
Milk

The text file can updated at runtime. In activity create I read the text file to the buttons
 
Upvote 0

Beja

Expert
Licensed User
Longtime User
Thanks Klaus, but I think I was trying to use the wrong example for my project..
Only a single button is defined here and then duplicated.. if need array of views then
CustomListView can not be easily modified unless it is rewritten from the start, bside the
initialization in activity create.
Thank you of course, and will use the sliding panels in the time being until figure out how
to use the listview.
 
Upvote 0

RandomCoder

Well-Known Member
Licensed User
Longtime User
... CustomListView can not be easily modified unless it is rewritten from the start, ...
With the CustomListView you add a Panel as an item in the list. The Panel can hold as many Views as you require and they can be arranged to suit your needs. It sounds as though you just want to add a button to each Panel, this is fine, but if you wanted you could also add a Label and ImageView etc.

Regards,
RandomCoder
 
Upvote 0

Beja

Expert
Licensed User
Longtime User
Thanks Randomcoder,

It sounds as though you just want to add a button to each Panel
Hi Randomcoder,
If you followed this thread, the buttons are already there. there are 10 buttons , but with the same text (caption) on them.
I want to put different text on each button. This is the problem. I can't without braking the code and do something new.
But I know this is how the example was done.. it is (by design) issue and I am just going to look for another solution
 
Upvote 0

mangojack

Expert
Licensed User
Longtime User
I can't without braking the code and do something new.
Not true ... As Klaus stated above you just have to adjust the code to to suit your needs... your not breaking it or rewriting from scratch .
In one app, each row of the CLV has an image and numerous labels populated from a sql db.

B4X:
Sub Activity_Create(FirstTime As Boolean)

   'Create 10 items made of a button, label and checkbox.
   clv3.Initialize(Me, "clv3")
   Activity.AddView(clv3.AsView, 0, 0%y, 100%x, 100%y)
  
   Dim mylist() As String = Array As String("Water", "Soda", "Bread", "Orange Juice", "Apple", "Ice Cubes", "French Fries", "Chicken", "Fish", "Milk")  
   Dim recData(2) As String
  
   For i = 1 To 10  
     recData(0) = mylist(i -1)
     recData(1) = "Label Caption # " & i
     clv3.Add(CreateListItem(recData, clv3.AsView.Width, 50dip), 50dip, "This is Return Value for Clicked Item #" & i)
   Next
End Sub

Sub clv3_ItemClick(Index As Int, Value As Object)
   Log(Index & " = " & Value)
End Sub

Sub CreateListItem(Values() As String, Width As Int, Height As Int) As Panel  '@@  Values() is the data passed from the clv3.Add(CtreateListItem .. call above
   Dim p As Panel
   p.Initialize("")
   p.Color = Colors.Black
  
   Dim b As Button
   b.Initialize("button")
   b.Text = Values(0)      ' @@ the button gets the first value in the passed array ...  Values()
  
   Dim lbl As Label
   lbl.Initialize("")
   lbl.Text = Values(1)     ' @@ the Label gets the 2nd value in the passed array ...  Values()
   lbl.TextSize = 16
   lbl.TextColor = Colors.White
  
   Dim chk As CheckBox
   chk.Initialize("chk")
  
   p.AddView(lbl, 5dip, 2dip, 150dip, Height - 4dip) 'view #0
   p.AddView(b, 155dip, 2dip, 110dip, Height - 4dip) 'view #1
   p.AddView(chk, 280dip, 2dip, 50dip, Height - 4dip) 'view #2
   Return p
End Sub
 
Upvote 0

Beja

Expert
Licensed User
Longtime User
@mangojack
I just tried your adjustment and getting ArrayIndexOutOfBoundexception.
I couldn't spot any anything wrong in the code though.. any meaning appreciated.
Project attached.
 

Attachments

  • outofbound.zip
    9.9 KB · Views: 224
Upvote 0
Top