NinePatchDrawable can not be cast... error

cmweb

Active Member
Licensed User
Longtime User
Hello,

I got an error report from a user of one of my apps.

The app uses Packagemanager to gather all installed apps and their icons. The error appears within that code...

Error:

Screenshot_2012-09-01-17-24-55.png



Log:

B4X:
I/ActivityManager( 7991): Displayed de.cmweb.batification/.edit: +560ms
D/dalvikvm(16260): GC_CONCURRENT freed 1794K, 32% free 5592K/8131K, paused 3ms+6ms
W/AssetRedirectionManager( 7991): Unable to attach target package assets for com.android.gallery3d
W/AssetRedirectionManager( 7991): Unable to attach target package assets for com.mobisystems.office
W/ResourceType(16260): getEntry failing because entryIndex 1 is beyond type entryCount 1
D/dalvikvm(16260): GC_FOR_ALLOC freed 1413K, 34% free 5522K/8263K, paused 20ms
D/dalvikvm(16260): GC_FOR_ALLOC freed <1K, 33% free 5539K/8263K, paused 14ms
I/dalvikvm-heap(16260): Grow heap (frag case) to 13.497MB for 36880-byte allocation
W/AssetRedirectionManager( 7991): Unable to attach target package assets for com.android.gallery3d
D/dalvikvm(16260): GC_CONCURRENT freed 547K, 29% free 6767K/9479K, paused 2ms+3ms
E/B4A (16260): edit_btnappselect_click (java line: 531)
E/B4A (16260):
E/B4A (16260): java.lang.ClassCastException: android.graphics.drawable.NinePatchDrawable cannot be cast to android.graphics.drawable.BitmapDrawable
E/B4A (16260): at de.cmweb.batification.edit._btnappselect_click(edit.java:531)
E/B4A (16260): at java.lang.reflect.Method.invokeNative(Native Method)
E/B4A (16260): at java.lang.reflect.Method.invoke(Method.java:511)
E/B4A (16260): at anywheresoftware.b4a.BA.raiseEvent2(BA.java:170)
E/B4A (16260): at anywheresoftware.b4a.BA.raiseEvent2(BA.java:158)
E/B4A (16260): at anywheresoftware.b4a.BA.raiseEvent(BA.java:154)
E/B4A (16260): at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:54)
E/B4A (16260): at android.view.View.performClick(View.java:3511)
E/B4A (16260): at android.view.View$PerformClick.run(View.java:14105)
E/B4A (16260): at android.os.Handler.handleCallback(Handler.java:605)
E/B4A (16260): at android.os.Handler.dispatchMessage(Handler.java:92)
E/B4A (16260): at android.os.Looper.loop(Looper.java:137)
E/B4A (16260): at android.app.ActivityThread.main(ActivityThread.java:4575)
E/B4A (16260): at java.lang.reflect.Method.invokeNative(Native Method)
E/B4A (16260): at java.lang.reflect.Method.invoke(Method.java:511)
E/B4A (16260): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
E/B4A (16260): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
E/B4A (16260): at dalvik.system.NativeStart.main(Native Method)
E/B4A (16260): java.lang.ClassCastException: android.graphics.drawable.NinePatchDrawable cannot be cast to android.graphics.drawable.BitmapDrawable

Code:


B4X:
Sub BtnAppSelect_Click
   If packlist.IsInitialized = False Then ' Begin Firsttime Package List
   
      packlist.Initialize("packlist")
      ProgressDialogShow("Loading list of installed apps..." & CRLF & "This might take a while...")
      packages = pm.GetInstalledPackages
      
      map1.Initialize
      pac.initialize
      For i = 0 To packages.Size -1 
         map1.Put(pm.GetApplicationLabel(packages.Get(i)),packages.Get(i))
         DoEvents
         pac.Add(pm.GetApplicationLabel(packages.Get(i)))
      Next
      
      pac.Sort(True)
      DoEvents
      
      For i = 0 To pac.Size - 1
         applabel = pac.get(i)
         packname = map1.Get(applabel)
         icons = pm.GetApplicationIcon(packname)
         DoEvents
         packlist.AddTwoLinesAndBitmap2(applabel,"",icons.bitmap,packname)
      Next
      ProgressDialogHide
      
      Activity.AddView(packlist,0,0,100%x,100%y)
      packlist.Visible = False
      packlist.BringToFront
      packlist.Color = Colors.black
   
   End If 'Ende First Time Packagelist

end sub


I can not reproduce that error message on my devices.

Any ideas, why that error appears?

Best regards,

Carsten
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
PackageManager.GetApplicationInfo returns a Drawable object. It might not be a BitmapDrawable.

You can test whether it is a BitmapDrawable with:
B4X:
If pm.GetApplicationIcon(packname) Is BitmapDrawable Then ...

If you want to convert a non BitmapDrawable to a BitmapDrawable then you need to:
- Create a mutable bitmap
- Draw the drawable with Canvas.DrawDrawable
 
Upvote 0

cmweb

Active Member
Licensed User
Longtime User
Thanks Erel, I understand...

Best regards,

Carsten

Gesendet von meinem GT-N7000 mit Tapatalk 2
 
Upvote 0

cmweb

Active Member
Licensed User
Longtime User
Hello Erel,

If you want to convert a non BitmapDrawable to a BitmapDrawable then you need to:
- Create a mutable bitmap
- Draw the drawable with Canvas.DrawDrawable
Could you please explain that to me?

I didn't work with mutable bitmaps and canvas so far...

Best regards,

Carsten
 
Upvote 0

cmweb

Active Member
Licensed User
Longtime User
Post the code that you are trying to build and I will further help you.

This is the code:

B4X:
If pm.GetApplicationIcon(packname) Is BitmapDrawable Then
            icons = pm.GetApplicationIcon(packname)
            packlist.AddTwoLinesAndBitmap2(applabel,"",icons.bitmap,packname)
         Else
            ' Convert to Bitmap and add it to the packlist Listview
         End If

Else should convert to bitmap and add it to the packlist.

The canvas.drawDrawable requires a RECT parameter and I don't know what that should be when the bitmap shoud just be added to a listview...

Thanks for your help.

Best regards,

Carsten
 
Upvote 0

Similar Threads

Top