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

还有其他相关问题,但是它们不能解决我的情况(它们的错误代码与客户端代码的某种回收和/或错误的强制调用有关)。

我的情况比较复杂。

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

问题是,它在我拥有的7部手机上都可以正常运行,并且全部运行API的19+版本。

但是,我有一个运行API 17的Google Nexus 4.3。我得到了这个崩溃日志,里面没有我的代码,只有库代码。如果您可以提出建议,我将不胜感激:

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)


更新资料

我最讨厌的课程是这样的:

 //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库,这是使用上述类的代码。

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

本文翻译自 https://stackoverflow.com/questions/31569468/

网站遵循 CC BY-SA 4.0 协议,转载或引用请注明出处。

标签 java android classcastexception android-framelayout android-runtime


相关文章:

java - 使用OrientDb的ClassCastException

java - Java日期库中一年中某周的不一致

java - 如何使RadioButton看起来像JavaFX中的常规Button

java - 卡夫卡制片人发送无效字符

c# - 从PC读取接收到Android手机的数据

android - Android自定义视图onDraw不起作用

android - 使用findViewById的随机ClassCastExceptions显然是随机视图

java - 带有Couchbase的Spring中的原子计数器

android - 如何在没有gcm注册的情况下解析发送推送?

java - 在Wildfly 8.2上重新部署后从Infinispan缓存中读取时发生ClassCastException