I'm trying to inflate a FrameLayout in a GridView and I'm getting an exception. Here is the getView method from the adapter (a subclass of BaseAdapter):
public View getView(int position, View convertView, ViewGroup parent) {
// TODO: Create compound view with image and name overlayed on top
FrameLayout personFrame;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
personFrame = (FrameLayout)inflater.inflate(R.layout.thumbnail, null);
//personFrame = (FrameLayout)View.inflate(mContext, R.layout.thumbnail, null);
personFrame.setLayoutParams(new GridView.LayoutParams(85,85));
} else {
personFrame = (FrameLayout) convertView;
}
.... and so on
Here's the layout I'm inflating:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="85px"
android:layout_height="85px">
<ImageView
android:id="@+id/thumbnail_image"
android:layout_height="85px"
android:padding="2px"
android:layout_width="85px"
android:scaleType="centerCrop" />
<TextView
android:id="@+id/person_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="2dip"
android:layout_gravity="center_horizontal|bottom"
android:padding="2dip"
android:background="#AA000000"
android:textColor="#ffffffff"
android:text="Me" />
</FrameLayout>
I've tried with and without setting the LayoutParams explicitly and I get the same exception each time, which is:
E/AndroidRuntime( 420): java.lang.ClassCastException: android.widget.FrameLayout$LayoutParams
E/AndroidRuntime( 420): at android.widget.GridView.onMeasure(GridView.java:934)
E/AndroidRuntime( 420): at android.view.View.measure(View.java:8171)
E/AndroidRuntime( 420): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
E/AndroidRuntime( 420): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1012)
E/AndroidRuntime( 420): at android.widget.LinearLayout.measureVertical(LinearLayout.java:381)
E/AndroidRuntime( 420): at android.widget.LinearLayout.onMeasure(LinearLayout.java:304)
E/AndroidRuntime( 420): at android.view.View.measure(View.java:8171)
E/AndroidRuntime( 420): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
E/AndroidRuntime( 420): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
E/AndroidRuntime( 420): at android.view.View.measure(View.java:8171)
E/AndroidRuntime( 420): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
E/AndroidRuntime( 420): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
E/AndroidRuntime( 420): at android.view.View.measure(View.java:8171)
E/AndroidRuntime( 420): at android.view.ViewRoot.performTraversals(ViewRoot.java:801)
E/AndroidRuntime( 420): at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
E/AndroidRuntime( 420): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 420): at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime( 420): at android.app.ActivityThread.main(ActivityThread.java:4627)
E/AndroidRuntime( 420): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 420): at java.lang.reflect.Method.invoke(Method.java:521)
E/AndroidRuntime( 420): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
E/AndroidRuntime( 420): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
E/AndroidRuntime( 420): at dalvik.system.NativeStart.main(Native Method)
Can anyone point out where I've gone wrong here? Many thanks
Actually returning the wrong object from getView (as you mentioned you were doing) is what caused the Exception. For example if you returned a button from within your FrameLayout instead of the FrameLayout itself this will cause the problem you describe. The GridView code is taking the View object you return and walking one step up the View hierarchy. There it expects to find an AbsListView (which it wraps each grid view item in). Because you returned a sub-element of your FrameLayout, the GridView class found a FrameLayout where it expected an AbsListView.
Try changing:
personFrame.setLayoutParams(new GridView.LayoutParams(85,85));
to:
personFrame.setLayoutParams(new AbsListView.LayoutParams(85,85));
It seems that when you set LayoutParams
of an object, it should be cast-able to the parent's LayoutParams
. In your case, the parent of personFrame
is the grid view.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With