1

Hi I have developed a Grid view with Images in it, I have written an adapter for it, it worked fine as the grid had just a single image, now I trying to put a text in the grid just below the image, Since then it is crashing , here is the crash log

    E/AndroidRuntime: FATAL EXCEPTION: main
    java.lang.ClassCastException: android.widget.AbsListView$LayoutParams cannot be cast    to android.widget.LinearLayout$LayoutParams
    at android.widget.LinearLayout.measureVertical(LinearLayout.java:655)
    at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
    at android.view.View.measure(View.java:15479)
    at android.widget.GridView.onMeasure(GridView.java:1044)
    at android.view.View.measure(View.java:15479)
    at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:617)
    at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:399)
    at android.view.View.measure(View.java:15479)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4828)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
    at android.view.View.measure(View.java:15479)
    at android.widget.LinearLayout.measureVertical(LinearLayout.java:833)
    at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
    at android.view.View.measure(View.java:15479)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4828)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2359)
    at android.view.View.measure(View.java:15479)
    at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1968)
    at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1214)
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1387)
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4464)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
    at android.view.Choreographer.doCallbacks(Choreographer.java:555)
    at android.view.Choreographer.doFrame(Choreographer.java:525)
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
    at android.os.Handler.handleCallback(Handler.java:615)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:4895)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
    at dalvik.system.NativeStart.main(Native Method)

Here is my custom adapter class

   public class ImageAdapter extends BaseAdapter
{
    private final String[] subText = {};
    Context mContext;

    public ImageAdapter(Context _context, String[] subText)
    {
        super();
        mContext = _context;
    }

    public static String[] mThumbIds ={};
    public static String[] mVideoIds = {};

    public static final String[] SubText = new String[] {
            "text1", "text2","text3", "text4","text1", "text2","text3", "text4" ,"text1", "text2","text3", "text4" ,"text1", "text2","text3", "text4"  };

     @Override
    public int getCount() {
    return mThumbIds.length;
}

    @Override
    public Object getItem(int position) {
    return mThumbIds[position];
}

    @Override
    public long getItemId(int position) {
    return 0;
}

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        // Add The Image!!!
        View gridView;

        LayoutInflater inflater = (LayoutInflater)mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        gridView = new View(mContext);

        gridView = inflater.inflate(R.layout.grid_item,null);

        //add Text
        TextView textView = (TextView)gridView.findViewById(R.id.grid_item_text);
        textView.setText(SubText[position]);


        ImageView imageView = (ImageView) gridView.findViewById(R.id.grid_item_image);
        imageView.setImageURI(Uri.parse(mThumbIds[position]));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setLayoutParams(new GridView.LayoutParams(150, 150));
        return gridView;
    }

}

Can anybody plase tell me where exactly I went wrong.

4

2 回答 2

1

问题很明显:您正在尝试将 android.widget.AbsListView$LayoutParams 转换为 android.widget.LinearLayout$LayoutParams。

改变这个:

imageView.setLayoutParams(new GridView.LayoutParams(150, 150));

有了这个:

imageView.setLayoutParams(new TheLayoutYouUseOnGrid_item.LayoutParams(150, 150));

我看到您正在为网格的每个项目R.layout.grid_item膨胀自定义布局。因此,当您更改 ImageView 上的参数时,您必须采用包含 ImageView 的 LayoutParams。

另外我认为您没有很好地编码 getView 方法。如果可以,请发布您的 grid_item 布局。

于 2013-07-26T13:26:56.120 回答
1

这条线

imageView.setLayoutParams(new GridView.LayoutParams(150, 150));

应该改为

gridView.setLayoutParams(new GridView.LayoutParams(150, 150));

LinearLayout错误是因为当 GridView 需要GridView布局参数时,您的自定义膨胀视图有一个参数

于 2013-07-26T13:27:02.400 回答