android.view.ViewGroup$LayoutParams 不能转换为 android.widget.AbsListView$LayoutParams

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21045198/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 03:53:46  来源:igfitidea点击:

android.view.ViewGroup$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams

android

提问by senzacionale

I am getting this expcetion:

我得到这个期望:

java.lang.ClassCastException: android.view.ViewGroup$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams
at android.widget.ListView.setupChild(ListView.java:1806)
at android.widget.ListView.makeAndAddView(ListView.java:1775)
at android.widget.ListView.fillDown(ListView.java:672)
at android.widget.ListView.fillFromTop(ListView.java:732)
at android.widget.ListView.layoutChildren(ListView.java:1625)
at android.widget.AbsListView.onLayout(AbsListView.java:1863)
at android.view.View.layout(View.java:11278)
at android.view.ViewGroup.layout(ViewGroup.java:4224)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1628)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1486)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1399)
at android.view.View.layout(View.java:11278)
at android.view.ViewGroup.layout(ViewGroup.java:4224)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1628)
at android.widget.LinearLayout.layoutHorizontal(LinearLayout.java:1617)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1401)
at android.view.View.layout(View.java:11278)
at android.view.ViewGroup.layout(ViewGroup.java:4224)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1628)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1486)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1399)
at android.view.View.layout(View.java:11278)
at android.view.ViewGroup.layout(ViewGroup.java:4224)
at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
at android.view.View.layout(View.java:11278)
at android.view.ViewGroup.layout(ViewGroup.java:4224)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1628)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1486)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1399)
at android.view.View.layout(View.java:11278)
at android.view.ViewGroup.layout(ViewGroup.java:4224)
at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
at android.view.View.layout(View.java:11278)
at android.view.ViewGroup.layout(ViewGroup.java:4224)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1504)
at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2458)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
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:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)

but the problem is that I can not find the root of the problem. Where exactly and what that means?

但问题是我找不到问题的根源。究竟在哪里,这意味着什么?

And why widget, i don't have any widget!

为什么小部件,我没有任何小部件!

CODE:

代码:

Not sure if here:

不确定这里是否:

public class MyListPreference extends ListPreference {

    public MyListPreference(Context context) {
        super(context);
    }

    public MyListPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected View onCreateDialogView() {
        TextView dialogTitle = (TextView)super.onCreateDialogView();

        if (dialogTitle != null) {
            // Dialog
            dialogTitle.setBackgroundColor(getContext().getResources().getColor(R.color.category_background));
            dialogTitle.setPadding(10, 4, 4, 4);

            // Text
            dialogTitle.setTextSize(14);
            dialogTitle.setTypeface(null, Typeface.BOLD);
            dialogTitle.setTextColor(getContext().getResources().getColor(R.color.actionbar_background));

            dialogTitle.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        }
        return dialogTitle;
    }
}

回答by laalto

dialogTitle.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

Here you're replacing existing layout params of correct type AbsListView.LayoutParamswith more generic ViewGroup.LayoutParams. Layout params type is that of the parent container of the view.

在这里,您将AbsListView.LayoutParams使用更通用的ViewGroup.LayoutParams. 布局参数类型是视图的父容器的类型。

If you need to modify existing layout params, access them with getLayoutParams(), modify it, and call requestLayout()to notify that the layout has changed.

如果您需要修改现有布局参数,请使用 访问它们getLayoutParams(),修改它,然后调用requestLayout()以通知布局已更改。

Example. Instead of

例子。代替

fooView.setLayoutParams(new LayoutParams(123, 456));

do

LayoutParams lp = fooView.getLayoutParams();
lp.width = 123;
lp.height = 456;
fooView.requestLayout();

回答by user10369036

Replace

代替

imageView.setLayoutParams(new LayoutParams(123, 456));

with

imageView.setLayoutParams(new  android.widget.AbsListView.LayoutParams(123, 456));

this worked for me

这对我有用

回答by thundertrick

This error is due to code inside ListView.

这个错误是由里面的代码引起的ListView

In conclusion, We should set only AbsListView.LayoutParamsto Viewif it is used directly as child in ListView.

总之,我们应该只设置AbsListView.LayoutParamsView如果它直接用作ListView.

Following is the reason.

原因如下。

In onMeasure()of ListView:

onMeasure()ListView

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//...
    // Lay out child directly against the parent measure spec so that
    // we can obtain exected minimum width and height.
    measureScrapChild(child, 0, widthMeasureSpec, heightSize);
//...
}

private void measureScrapChild(View child, int position, int widthMeasureSpec, int heightHint) {
    LayoutParams p = (LayoutParams) child.getLayoutParams();
    if (p == null) {
        p = (AbsListView.LayoutParams) generateDefaultLayoutParams();
        child.setLayoutParams(p);
    }
    //...
}

So if an item view already has ViewGroup.LayoutParams, it will not be override to AbsListView.LayoutParams. However, in setupChild, layoutChildrenand many other methods, ListViewalways casts LayoutParamsof item view into AbsListView.LayoutParams. Such as

因此,如果项目视图已经具有ViewGroup.LayoutParams,则不会覆盖到AbsListView.LayoutParams。然而,在setupChildlayoutChildren和许多其他的方法,ListView总是蒙上LayoutParams项目视图成AbsListView.LayoutParams。如

private void setupChild(View child, int position, int y, boolean flowDown, int childrenLeft,
            boolean selected, boolean isAttachedToWindow) {
    //...

    // Respect layout params that are already in the view. Otherwise make
    // some up...
    AbsListView.LayoutParams p = (AbsListView.LayoutParams) child.getLayoutParams();
    if (p == null) {
        p = (AbsListView.LayoutParams) generateDefaultLayoutParams();
    }
    //...
}