android.view.ViewGroup $ LayoutParams无法强制转换为android.widget.AbsListView $ LayoutParams

时间:2014-01-10 13:08:57

标签: android

我得到了这个说法:

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)

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

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

CODE:

不确定是否在这里:

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

3 个答案:

答案 0 :(得分:48)

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

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

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

实施例。而不是

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

DO

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

答案 1 :(得分:0)

替换

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

使用

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

这对我有用

答案 2 :(得分:0)

此错误是由于ListView中的代码引起的。

最后,如果在AbsListView.LayoutParams中直接将View设置为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); } //... } 的{​​{1}}中:

ViewGroup.LayoutParams

因此,如果项目视图已经具有AbsListView.LayoutParams,则不会被覆盖到setupChild。但是,在layoutChildrenListView和许多其他方法中,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(); } //... } 中。如

android.content.res.Resources$NotFoundException: Resource ID #0x7e070000
android.app.ActivityThread.performLaunchActivity