android.widget.FrameLayout$LayoutParams 无法转换为 android.widget.AbsListView$LayoutParams

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

还有其他相关问题,但它们没有解决我的情况(它们的错误代码与某种回收有关 and/or 客户端代码的错误转换调用)。

我的情况比较复杂

我有一些代码,用户可以在其中调出照片库。

问题是,它在我拥有的 7 部手机上运行良好,所有 运行 API 都是 19 岁以上。

但是我有一个 Google Nexus 4.3,它是 运行 API 17。我得到这个崩溃日志,其中有 none 我的代码,只有库代码。如果您能建议我如何编写解决方案,我会洗耳恭听:

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)

更新

我的冒犯class是这样的:

 //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);
        }
}

我正在使用 Ultimate Image Loader 库,这是使用上述 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;
        }

所以事实证明 API < 17 FrameLayout 无法正确转换为 AbsListView 参数。

我所做的是设置 AbsListView LayoutParams 而不是 FrameLayout 参数:

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

我不确定这是否对您有帮助或有什么帮助,但是当我使用 AndroidSlidingUpPanel 并以编程方式在其内容布局中添加视图时,我曾经发生过这种崩溃。在搜索并尝试了许多不同的解决方案之后,我想出了这个解决方案;我将以下布局添加为内容布局的第一个子布局,崩溃已解决。

<android.widget.AbsListView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:visibility="gone"
    app:layout_constraintTop_toTopOf="parent" />