ClassCastException in android (pre 3.0) when loading Drawable

I was trying to load a Drawable object for use in a project based on Android 2.3.3, but I kept getting this error:

java.lang.ClassCastException: android.graphics.drawable.ColorDrawable
at bv.getView(BackgroundGallery.java:163)
at android.widget.Gallery.makeAndAddView(Gallery.java:748)
at android.widget.Gallery.fillToGalleryRight(Gallery.java:700)
at android.widget.Gallery.layout(Gallery.java:631)
at android.widget.Gallery.onLayout(Gallery.java:339)
at android.view.View.layout(View.java:7228)
at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
at android.view.View.layout(View.java:7228)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1130)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1047)
at android.view.View.layout(View.java:7228)
at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
at android.view.View.layout(View.java:7228)
at android.view.ViewRoot.performTraversals(ViewRoot.java:1157)
at android.view.ViewRoot.handleMessage(ViewRoot.java:1877)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3687)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)

I went back, checked the image and everything seemed to be ok. Getting frustrated I started looking online for answers.
Finally I found a bug report with others having the same issue.
It turns out that on Android versions before 3.0 there is a bug in the OS which causes the first Drawable in the resources folder to be loaded as a ColorDrawable.
Obviously this will cause an error if you’re just trying to load an image.
The solution for me was to create a dummy image named a.png and placing it in res/drawable, making sure to never use it.
After that I was able to use my other images without any issues.
Original bug report

Share

Problems with proguard and the android compatibility package

I’ve been playing with Fragments lately and so I’m using the android support package so I can get Fragments in pre 3.0 version of Android.
However when I build the app proguard complains about being unable to find some classes referenced by the compatibility package.
Here are some of the warnings proguard throws:
[proguard] Warning: android.support.v4.widget.EdgeEffectCompatIcs: can't find referenced class android.widget.EdgeEffect
[proguard] Warning: android.support.v4.widget.EdgeEffectCompatIcs: can't find referenced class android.widget.EdgeEffect
[proguard] Warning: android.support.v4.view.MenuCompatHoneycomb: can't find referenced method 'void setShowAsAction(int)' in class android.view.MenuItem
[proguard] Warning: android.support.v4.view.ViewGroupCompatIcs: can't find referenced method 'boolean onRequestSendAccessibilityEvent (android.view.View,android.view.accessibility.AccessibilityEvent)' in class android.view.ViewGroup
[proguard] Warning: android.support.v4.view.accessibility.AccessibilityManagerCompatIcs$1: can't find referenced class android.view.accessibility.AccessibilityManager$AccessibilityStateChangeListener
[proguard] Note: the configuration refers to the unknown class 'android.widget.EdgeEffect'
[proguard] Note: the configuration refers to the unknown class 'android.view.accessibility.AccessibilityRecord'

That looks to me like the support package is trying to use some Honeycomb only classes.
After some research I found that the easiest way to get rid of those warnings is to have proguard not warn about the missing classes.
To do that, I added this line to my proguard.cfg file:

-dontwarn android.support.v4.**

If you can narrow it down to only a certain package you can ignore just that more specific package, but I would’ve had to make 3 new rules for proguard in my case.

Another way to fix those warnings is to change the API version you’re targeting (if you have that possibility).
API version 11 is Honeycomb so that should have all the missing classes.

Share

How to obtain user location on Google TV

I was searching for a way to get a user’s location on the Logitech Revue but I couldn’t find anything in the google documentation for google tv.
I tried getting the location based on the network since google tv doesn’t have access to a GPS, but that didn’t work.
After searching online I finally figured out how to get the user location on the Revue.
When getting the user’s last known location from the LocationManager in android you pass in a type of location you want (such as LocationManager.NETWORK_PROVIDER).
On the google tv there isn’t currently a defined type so you pass in the string “static” to the getLastKnownLocation method.
There are some permissions you need to set in your AndroidManifest.xml so you can access the location:

<uses-feature android:name="android.hardware.location" android:required="false"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

And the code to actually get the location is:

LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
Location loc = locationManager.getLastKnownLocation("static");

Source

Share