Android Question getting all views recursive

Anthony Araya

Member
Licensed User
hello, i have a problem because im trying get all views and reload an url or html stored in the webview.tag but when im debugging there's an error
when im debbugging sometimes works, but when i release it always fails
this is my code

B4X:
Sub Activity_Resume
    Dim vText As String =""

    If vPanel.IsInitialized Then
        For Each v As WebView In vPanel.GetAllViewsRecursive
            If v.IsInitialized Then
                vText=v.Tag

                If vText.Trim.Length >0 And vText.ToLowerCase<>"null" Then
                    If vText.Contains("||url||") Then
                        v.Loadurl(vText.Replace("||url||",""))
                    End If

                    If vText.Contains("||html||") Then
                        v.LoadHtml(vText.Replace("||html||",""))
                    End If
                End If
            End If
        Next
    End If

End Sub


And this is the error
java.lang.ClassCastException: anywheresoftware.b4a.BALayout cannot be cast to android.webkit.WebView
at b4a.example.news$ResumableSub_Activity_Resume.resume(news.java:621)
at anywheresoftware.b4a.shell.DebugResumableSub$DelegatableResumableSub.resumeAsUserSub(DebugResumableSub.java:48)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:348)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:180)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:176)
at anywheresoftware.b4a.shell.DebugResumableSub$DelegatableResumableSub.resume(DebugResumableSub.java:43)
at anywheresoftware.b4a.keywords.Common$13.run(Common.java:1705)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7050)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965)


thank you for supporting me
 
Last edited:

Computersmith64

Well-Known Member
Licensed User
Longtime User
hello, i have a problem because im trying get all views and reload an url or html stored in the webview.tag but when im debugging there's an error this is my code
Sub Activity_Resume
Dim vText As String =""
If vPanel.IsInitialized Then
For Each v As WebView In vPanel.GetAllViewsRecursive
If v.IsInitialized Then
vText=v.Tag
If vText.Trim.Length >0 And vText.ToLowerCase<>"null" Then
If vText.Contains("||url||") Then
v.Loadurl(vText.Replace("||url||",""))
End If
If vText.Contains("||html||") Then
v.LoadHtml(vText.Replace("||html||",""))
End If
End If
End If
Next
End If
End Sub


And this is the error
java.lang.ClassCastException: anywheresoftware.b4a.BALayout cannot be cast to android.webkit.WebView
at b4a.example.news$ResumableSub_Activity_Resume.resume(news.java:621)
at anywheresoftware.b4a.shell.DebugResumableSub$DelegatableResumableSub.resumeAsUserSub(DebugResumableSub.java:48)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:348)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:180)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:176)
at anywheresoftware.b4a.shell.DebugResumableSub$DelegatableResumableSub.resume(DebugResumableSub.java:43)
at anywheresoftware.b4a.keywords.Common$13.run(Common.java:1705)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7050)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965)


thank you for supporting me
Firstly you need to use the code tags when you're posting code in the forum, otherwise it makes it really hard to read...

OK - unless the panel only has a single view (& it's a webview), you need to change your loop (note the code tags :)):
B4X:
Sub Activity_Resume
    Dim vText As String =""
    If vPanel.IsInitialized Then
        For Each v As View In vPanel.GetAllViewsRecursive
            If v is WebView Then
                vText=v.Tag
                If vText.Trim.Length >0 And vText.ToLowerCase<>"null" Then
                    If vText.Contains("||url||") Then
                        v.Loadurl(vText.Replace("||url||",""))
                    End If
                    If vText.Contains("||html||") Then
                        v.LoadHtml(vText.Replace("||html||",""))
                    End If
                End If
            End If
        Next
    End If
End Sub

Having said that, do you have more than 1 WebView you're trying to access? If not, then why don't you just reference the WebView directly:
B4X:
Sub Activity_Resume
    Dim vText As String =""
  
                vText=WebView.Tag
                If vText.Trim.Length >0 And vText.ToLowerCase<>"null" Then
                    If vText.Contains("||url||") Then
                        WebView.Loadurl(vText.Replace("||url||",""))
                    End If
                    If vText.Contains("||html||") Then
                        WebView.LoadHtml(vText.Replace("||html||",""))
                    End If
                End If
     
End Sub

- Colin.
 
Upvote 0

Anthony Araya

Member
Licensed User
sorry im new hehe, but there's a main panel, it has many others panels created by code, some of them have a webview, but some of them have stored an url or html code, so im trying to load them correctly
 
Upvote 0

Computersmith64

Well-Known Member
Licensed User
Longtime User
sorry im new hehe, but there's a main panel, it has many others panels created by code, some of them have a webview, but some of them have stored an url or html code, so im trying to load them correctly
OK - so the loop I posted should work. I haven't tested it though (obviously), so it might need some tweaking...

- Colin.
 
Upvote 0

Computersmith64

Well-Known Member
Licensed User
Longtime User
i have to use a auxiliary webview because v is a view and not a webview, but it works, thank you very much
Oops - yeah, you're right. Something like:
B4X:
Sub Activity_Resume
    Dim vText As String =""
    If vPanel.IsInitialized Then
        For Each v As View In vPanel.GetAllViewsRecursive
            If v is WebView Then
                Private vWebView As WebView = v   'Assign the View to a WebView
                vText=v.Tag
                If vText.Trim.Length >0 And vText.ToLowerCase<>"null" Then
                    If vText.Contains("||url||") Then
                        v.Loadurl(vText.Replace("||url||",""))
                    End If
                    If vText.Contains("||html||") Then
                        v.LoadHtml(vText.Replace("||html||",""))
                    End If
                End If
            End If
        Next
    End If
End Sub

- Colin.
 
Upvote 0
Top