ClassCastException while inflating FrameLayout in GridView

I am trying to inflate FrameLayout in a GridView and I am getting an exception. Here is the getView method from the adapter (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;
    }

.... etc.

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 tried with and without installing LayoutParams explicitly, and each time I get the same exception:

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 someone please indicate where I made a mistake here? Many thanks

+5
source share
4 answers

GridView is not FrameLayout, both are ViewGroups, but it is not FrameLayout.

-4
source

getView ( , ) , . , FrameLayout FrameLayout, . GridView View, , View. AbsListView ( ). FrameLayout, GridView FrameLayout, AbsListView.

+18

:

personFrame.setLayoutParams(new GridView.LayoutParams(85,85));

personFrame.setLayoutParams(new AbsListView.LayoutParams(85,85));

, LayoutParams , LayoutParams. personFrame - .

+9

Well, I don’t know what was happening, but removing setLayoutParams fixed that. I do not know how to set these parameters in the code, but it does not seem necessary, so it’s good. I also made a stupid mistake in returning the wrong object from the getView method, but this did not affect this failure.

0
source

All Articles