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

前端之家收集整理的这篇文章主要介绍了android.view.ViewGroup $LayoutParams不能被转换为android.widget.AbsListView $LayoutParams前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我得到这个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)

但问题是我找不到问题的根源.究竟在哪里呢?

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

码:

不确定在这里:

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

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

解决方法

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

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

如果您需要修改现有的布局参数,请使用getLayoutParams()访问它们,修改它,并调用requestLayout()来通知布局已更改.

例.代替

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

LayoutParams lp = fooView.getLayoutParams();
lp.width = 123;
lp.height = 456;
fooView.requestLayout();
原文链接:https://www.f2er.com/android/312714.html

猜你在找的Android相关文章