Android Question ResumableSub cannot be cast to Drawable

Brandsum

Well-Known Member
Licensed User
Hi,
I'm using LabelExtra for showing some HTML content. Everything is working fine but getting an error while showing img tag.

Here is the code:
B4X:
Dim htmlParser As Html
Dim desc As Label
desc.Initialize("")
desc.Text = htmlParser.FromHtml2(htmlString,"ImageGetter","TagGetter")


Sub ImageGetter_GetDrawable(Source As String) As Object
    Dim data As Object =  getImage(Source)
    If data=Null Then
        Return Null
    Else
        Dim b As BitmapDrawable = data '<- getting error on this line
        Return b
    End If
End Sub

Sub getImage(Source As String) As ResumableSub
    Dim j As HttpJob
    Dim BitmapDrawable1 As BitmapDrawable
    j.Initialize("", Me)

    j.Download(Source)
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        BitmapDrawable1.Initialize(j.GetBitmap)
        Dim l As LabelExtras:l.SetDrawableBounds(BitmapDrawable1, 0, 0, j.GetBitmap.Width, j.GetBitmap.Height)
        j.Release
        Return BitmapDrawable1
    Else
        j.Release
        Return Null
    End If
End Sub

Getting this error:
B4X:
java.lang.ClassCastException: anywheresoftware.b4a.keywords.Common$ResumableSubWrapper cannot be cast to android.graphics.drawable.Drawable

If I use wait for (getImage) complete(result as object) then I have to change the return type of Sub ImageGetter_GetDrawable(Source As String) As Object to resumable sub. But as per the library example this return type has to be an Object.

So is there any solution?
 

Brandsum

Well-Known Member
Licensed User
B4X:
Wait For (getImage) complete(result as BitmapDrawable)
If I use this code inside ImageGetter_GetDrawable then the return type is changing to Resumable sub. But it has to be Object. So it is throwing
B4X:
java.lang.ClassCastException: anywheresoftware.b4a.keywords.Common$ResumableSubWrapper cannot be cast to android.graphics.drawable.Drawable
B4X:
Sub ImageGetter_GetDrawable(Source As String) As ResumableSub '<- this must be object type
    tempSource = Source
    wait for (getImage) complete(data As BitmapDrawable)
    Log(data)
    Return data '<-error  at this line
End Sub
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
the return type is changing to Resumable sub. But it has to be Object.
I think there is a misunderstanding here that is clouding your view of whatever the real problem is. A ResumableSub type is in fact an Object type. Object is the root type that all Object types derive from and has a very limited set of methods that every Object inherits. You can pass anything as an Object which in fact is the purpose of defining a parameter type as Object. If you know what the actual type is you can then assign the object to a variable Dimmed as the actual type (this is sometimes called 'casting') and access all of its methods.
 
Upvote 0

Brandsum

Well-Known Member
Licensed User
Thank you all for helping. I just solved this issue by downloading all the images before setting the HTML content via fromHtml2 method.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…