我正在尝试在GridView中填充FrameLayout,但是出现了异常。以下是适配器(BaseAdapter的子类)中的getView方法:
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;
}
这是我正在加载的布局:
<?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>
我尝试过显式设置LayoutParams和不设置,但每次都遇到相同的异常,异常内容如下:
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)
有人能指出我在这里做错了什么吗?非常感谢。