For some reason my Android project compiles properly, runs fine on both phones and emulators, and even includes multiple XML layout files with LinearLayouts, TextViews, Buttons, Images, and even custom View components, but when it comes to shapes it simply can’t find them.
Heres my code:
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<solid android:color="#ffffff"/>
<stroke android:width="3dp"
android:color="#ff000000"/>
<padding android:left="1dp"
android:top="1dp"
android:right="1dp"
android:bottom="1dp"/>
<corners android:radius="30dp"/>
</shape>
And the error I get in the graphical layout in eclipse:
com.android.layoutlib.bridge.MockView cannot be cast to android.view.ViewGroup
Exception details are logged in Window > Show View > Error Log
The following classes could not be found:
- shape (Fix Build Path, Edit XML)
- solid (Fix Build Path, Edit XML)
I’m targeting Android 2.2 Froyo Google APIs.
Suggestions?
You’re probably getting the error because you’re putting this XML in the wrong place. The XML above should go in the
res/drawabledirectory. You really should not draw a square in the Layout. Draw a Square in a View and add it to the Layout. Or use an image and add it to a View via an ImageView. A Layout is an aggregation of Views. If you really want to draw a Square in a Layout (ViewGroup) you can extend one and draw it in the onDraw() method. But that’ll probably be more complicated than needed. Have a look at the the Android Docs on Layouts: http://developer.android.com/guide/topics/ui/index.html