senzacionale
senzacionale

Reputation: 20906

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

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

Upvotes: 18

Views: 36619

Answers (3)

thundertrick
thundertrick

Reputation: 1674

This error is due to code inside ListView.

In conclusion, We should set only AbsListView.LayoutParams to View if it is used directly as child in ListView.

Following is the reason.

In onMeasure() of 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, layoutChildren and many other methods, ListView always casts LayoutParams of item view into AbsListView.LayoutParams. Such as

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

Upvotes: 0

user10369036
user10369036

Reputation: 11

Replace

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

with

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

this worked for me

Upvotes: 0

laalto
laalto

Reputation: 152787

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

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

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

Example. Instead of

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

do

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

Upvotes: 51

Related Questions