在Android ClassCastException中的GridView

时间:2015-03-17 14:58:55

标签: android gridview android-asynctask classcastexception

我正在尝试修改Google样本http://developer.android.com/training/displaying-bitmaps/process-bitmap.html。 此示例使用asynctask从Internet获取图像并在gridview中显示。我想要做的是在图像下方显示文字。我得到例外java.lang.ClassCastException: android.widget.AbsListView$LayoutParams cannot be cast to android.widget.LinearLayout$LayoutParams

以下是代码:

  1. getView方法

    @Override
    public View getView(int position, View convertView, ViewGroup container) {
        //BEGIN_INCLUDE(load_gridview_item)
        // First check if this is the top row
        if (position < mNumColumns) {
            if (convertView == null) {
                convertView = new View(mContext);
            }
            // Set empty view with height of ActionBar
            convertView.setLayoutParams(new AbsListView.LayoutParams(
                    LayoutParams.MATCH_PARENT, mActionBarHeight));
            return convertView;
        }
    
        // Now handle the main ImageView thumbnails
        View vi = convertView;
        LayoutInflater inflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    
        if (convertView == null)  // if it's not recycled, instantiate and initialize
    
            vi = inflater.inflate(R.layout.row_grid, null);
            TextView textView = (TextView) vi.findViewById(R.id.grid_text);
            ImageView imageView = (ImageView) vi.findViewById(R.id.grid_image);
            textView.setText("Sample text");
            //imageView = new RecyclingImageView(mContext);
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setLayoutParams(mImageViewLayoutParams);
    
    
            // Check the height matches our calculated column width
        if (imageView.getLayoutParams().height != mItemHeight) {
            imageView.setLayoutParams(mImageViewLayoutParams);
        }
    
            // Finally load the image asynchronously into the ImageView, this also takes care of
            // setting a placeholder image while the background thread runs
            mImageFetcher.loadImage(Images.imageThumbUrls()[position - mNumColumns], imageView);
            return vi;
            //END_INCLUDE(load_gridview_item)
    
    }
    
  2. 异常

    java.lang.ClassCastException:android.widget.AbsListView $ LayoutParams无法强制转换为android.widget.LinearLayout $ LayoutParams         在android.widget.LinearLayout.measureVertical(LinearLayout.java:651)         在android.widget.LinearLayout.onMeasure(LinearLayout.java:558)         在android.view.View.measure(View.java:12853)         在android.widget.GridView.setupChild(GridView.java:1425)         在android.widget.GridView.makeAndAddView(GridView.java:1342)         在android.widget.GridView.makeRow(GridView.java:344)         在android.widget.GridView.fillDown(GridView.java:286)         在android.widget.GridView.fillFromTop(GridView.java:419)         在android.widget.GridView.layoutChildren(GridView.java:1231)         在android.widget.AbsListView.onLayout(AbsListView.java:1875)         在android.view.View.layout(View.java:11390)         在android.view.ViewGroup.layout(ViewGroup.java:4332)         在android.widget.LinearLayout.setChildFrame(LinearLayout.java:1653)         在android.widget.LinearLayout.layoutHorizo​​ntal(LinearLayout.java:1642)         在android.widget.LinearLayout.onLayout(LinearLayout.java:1418)         在android.view.View.layout(View.java:11390)         在android.view.ViewGroup.layout(ViewGroup.java:4332)         在android.widget.FrameLayout.onLayout(FrameLayout.java:444)         在android.view.View.layout(View.java:11390)         在android.view.ViewGroup.layout(ViewGroup.java:4332)         在android.widget.FrameLayout.onLayout(FrameLayout.java:444)         在android.view.View.layout(View.java:11390)         在android.view.ViewGroup.layout(ViewGroup.java:4332)         在android.widget.FrameLayout.onLayout(FrameLayout.java:444)         在android.view.View.layout(View.java:11390)         在android.view.ViewGroup.layout(ViewGroup.java:4332)         在android.widget.FrameLayout.onLayout(FrameLayout.java:444)         在android.view.View.layout(View.java:11390)         在android.view.ViewGroup.layout(ViewGroup.java:4332)         在android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1490)         在android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2455)         在android.os.Handler.dispatchMessage(Handler.java:99)         在android.os.Looper.loop(Looper.java:137)         在android.app.ActivityThread.main(ActivityThread.java:4424)         at java.lang.reflect.Method.invokeNative(Native Method)         在java.lang.reflect.Method.invoke(Method.java:511)         在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:817)         在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)         在dalvik.system.NativeStart.main(本地方法)

  3. ImageGridFragment.xml

  4. <GridView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/gridView"
        style="@style/PhotoGridLayout"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:columnWidth="@dimen/image_thumbnail_size"
        android:horizontalSpacing="@dimen/image_thumbnail_spacing"
        android:numColumns="auto_fit"
        android:stretchMode="columnWidth"
        android:verticalSpacing="@dimen/image_thumbnail_spacing" >
    
    </GridView>
    
    1. row_gridview.xml
    2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:padding="5dp"
          android:orientation="vertical">
          <ImageView
              android:id="@+id/grid_image"
              android:layout_width="50dp"
              android:layout_height="50dp"
              android:background="#FF0000"/>
          <TextView
              android:id="@+id/grid_text"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_marginTop="15dp"
              android:textSize="9sp"/>
      
      </LinearLayout>
      

1 个答案:

答案 0 :(得分:0)

尝试使用

convertView.setLayoutParams(new LinearLayout.LayoutParams(
            LayoutParams.MATCH_PARENT, mActionBarHeight));

而不是

convertView.setLayoutParams(new AbsListView.LayoutParams(
            LayoutParams.MATCH_PARENT, mActionBarHeight));