android.widget.FrameLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams

Aggressor picture Aggressor · Jul 22, 2015 · Viewed 10.1k times · Source

There are other related issues on this but they do not address my situation (their error code has to do with some sort of recycling and/or bad cast calls by client code).

My situation is more complex.

I have some code where the user can pull up a library of photos.

The thing is, its working just fine on 7 phones that I have, all running API's 19+.

However I have a Google Nexus 4.3 that's running API 17. And I get this crash log which has none of my code, only library code. If you can advise how I might be able to code a work around I'd be all ears:

FATAL EXCEPTION: main
    java.lang.ClassCastException: android.widget.FrameLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams
            at android.widget.GridView.onMeasure(GridView.java:1042)
            at android.view.View.measure(View.java:15848)
            at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:728)
            at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:477)
            at android.view.View.measure(View.java:15848)
            at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:728)
            at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:477)
            at android.view.View.measure(View.java:15848)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5008)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
            at android.view.View.measure(View.java:15848)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5008)
            at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
            at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
            at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
            at android.view.View.measure(View.java:15848)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5008)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
            at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2189)
            at android.view.View.measure(View.java:15848)
            at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1905)
            at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1104)
            at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1284)
            at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
            at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
            at android.view.Choreographer.doCallbacks(Choreographer.java:562)
            at android.view.Choreographer.doFrame(Choreographer.java:532)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
            at android.os.Handler.handleCallback(Handler.java:730)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5103)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)

Update

My offending class is this:

 //Special class that works with the UIL
    //The holder gets re-used and gets the new properties from this
    static class ReusableGridViewView extends FrameLayout
    {
        public ImageView imageView;
        public ProgressBar progressBar;

        public ReusableGridViewView(Context context)
        {
            super(context);

            setLayoutParams(new LayoutParams((int)(0.325* ViewHelper.screenWidthPX(context)),(int)(0.325* ViewHelper.screenWidthPX(context))));

            imageView = new ImageView(context);
            progressBar = new ProgressBar(context);

            addView(imageView);
            addView(progressBar);
        }
}

Im using the Ultimate Image Loader library and this is the code that uses the above class.

 //I do NOT call this directly, the listview decides when re use a convert view and passes it accordingly
        @Override
        public View getView(int position, View convertView, ViewGroup parent)
        {
            final ViewHolder holder;
            View view = convertView;
            if (view == null)
            {
                view = new ReusableGridViewView(parent.getContext());

                holder = new ViewHolder();
                assert view != null;
                holder.imageView = ((ReusableGridViewView)view).imageView;
                holder.progressBar = ((ReusableGridViewView)view).progressBar;
                holder.imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                view.setTag(holder);
            } else {
                holder = (ViewHolder) view.getTag();
            }

            //System.out.println(" " + holder + " ||| " + holder.imageView);
            ImageLoader.getInstance()
                    .displayImage(IMAGE_URLS[position], holder.imageView, options, new SimpleImageLoadingListener()
                    {
                        @Override
                        public void onLoadingStarted(String imageUri, View view)
                        {
                            holder.progressBar.setProgress(0);
                            holder.progressBar.setVisibility(View.VISIBLE);
                        }

                        @Override
                        public void onLoadingFailed(String imageUri, View view, FailReason failReason)
                        {
                            holder.progressBar.setVisibility(View.GONE);
                        }

                        @Override
                        public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage)
                        {
                            holder.progressBar.setVisibility(View.GONE);
                        }

                    }, new ImageLoadingProgressListener()
                    {
                        @Override
                        public void onProgressUpdate(String imageUri, View view, int current, int total)
                        {
                            holder.progressBar.setProgress(Math.round(100.0f * current / total));
                        }
                    });

            //set all as unselected
            ((ReusableGridViewView) view).removeSelectionBorder();
            for(int i = 0; i < SELECTED_IMAGES.length; i++)
            {
                if(SELECTED_IMAGES[i] == position)
                {
                    //set cell to selected
                    ((ReusableGridViewView) view).addSelectionBorder();
                }
            }
            return view;
        }

Answer

Aggressor picture Aggressor · Aug 4, 2015

So it turns out that on API < 17 FrameLayout can't be cast correctly to AbsListView params.

What I did was set AbsListView LayoutParams instead of FrameLayout params:

    setLayoutParams(new AbsListView.LayoutParams(100,100));