Hope that someone can help me here. I populate a scrollview with a series of labels. When one of the labels is to be deleted, I want to clear all the labels and redraw them again in the new order.
This is a simple chat program and these are the messages.
The populate code is here:
And the code to remove is here:
Error Log:
Screenshots are also enclosed.
I hope this is a simple error but I am so blind I can't see it.
Thanks
Mark
Edit: Solved the problem by removing the whole RemoveOldMessages sub. Now it works butI don't know why????
This is a simple chat program and these are the messages.
The populate code is here:
B4X:
Sub JobDone(job As HttpJob)
ProgressDialogHide
If job.Success Then
Dim res As String
res = job.GetString
Log("Response from server: " & res)
Dim parser As JSONParser
parser.Initialize(res)
Select job.JobName
Case "GetAllMessages"
#Region
Dim ColorFlag, CurrentTop, CurrentLeft As Int
ColorFlag=1
CurrentTop=1
messages=parser.NextArray 'returns alist with maps
For i=0 To messages.Size-1
Dim m As Map
m=messages.Get(i)
msgID=m.Get("msgID")
name=m.Get("name")
email=m.Get("email")
msgFrom=m.Get("msgFrom")
msgDate=m.Get("msgDate")
msgBody=m.Get("msgBody")
Dim msgLabel As Label
msgLabel.Initialize("msgLabel")
'Create Label text
msgLabel.Text="<" & msgID & "> " & name & " from " & msgFrom & " @" & msgDate & CRLF & msgBody
'set text color and left position
If ColorFlag<0 Then
msgLabel.Color=Colors.Green
CurrentLeft=10
Else
msgLabel.Color=Colors.Yellow
CurrentLeft=40
End If
msgView.Panel.AddView(msgLabel,CurrentLeft,CurrentTop, 100%x-60, 10%y)
msgLabel.Tag=msgID
Dim su As StringUtils
msgLabel.Height = su.MeasureMultilineTextHeight(msgLabel, msgLabel.Text)
msgLabel.TextColor=Colors.Black
'Prepare the top of the next label
CurrentTop=CurrentTop+msgLabel.Height+5
msgView.Panel.Height=CurrentTop
ColorFlag=ColorFlag*-1
Next
msgView.FullScroll(True)
#End Region
Case "AddMessage"
FetchMessages 'RemoveOldMessages is the first command in this sub
Case "DeleteMessage"
FetchMessages
End Select
Else
ToastMessageShow("Error: " & job.ErrorMessage, True)
End If
job.Release
End Sub
And the code to remove is here:
B4X:
Sub RemoveOldMessages
'Delete all views in scrollview
' Log("No. of Views: " & msgView.Panel.NumberOfViews)
If msgView.Panel.NumberOfViews=0 Then Return 'Can't delete whats not there!
For i=0 To msgView.Panel.NumberOfViews-1
msgView.Panel.GetView(i).RemoveView 'This is line 47 and the screenshot was taken at i=3
Next
End Sub
Error Log:
B4X:
main_removeoldmessages (B4A line: 47)
msgView.Panel.GetView(i).RemoveView
java.lang.RuntimeException: Object should first be initialized (View).
Did you forget to call Activity.LoadLayout?
at anywheresoftware.b4a.AbsObjectWrapper.getObject(AbsObjectWrapper.java:45)
at anywheresoftware.b4a.objects.ViewWrapper.RemoveView(ViewWrapper.java:225)
at mrapps.mychat.main._removeoldmessages(main.java:702)
at mrapps.mychat.main._fetchmessages(main.java:358)
at mrapps.mychat.main._jobdone(main.java:612)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:170)
at anywheresoftware.b4a.keywords.Common$4.run(Common.java:881)
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).
Did you forget to call Activity.LoadLayout?
Screenshots are also enclosed.
I hope this is a simple error but I am so blind I can't see it.
Thanks
Mark
Edit: Solved the problem by removing the whole RemoveOldMessages sub. Now it works butI don't know why????
Attachments
Last edited: