[工作记录]ListView添加FootView,LayoutParams转化出错

在工作中开发一款app,运行在我的手机魅蓝note(系统5.1)上没有问题,运行在同事的小米4上发生crash.我用红米note(系统4.4)测试也发生crash。得到入下日志:

FINGERPRINT: Xiaomi/gucci/gucci:4.4.4/KTU84P/V7.2.1.0.KHKCNDA:user/release-keys
HARDWARE: qcom
HOST: zc-miui-ota-bd35.bj
ID: KTU84P
MANUFACTURER: Xiaomi
MODEL: HM NOTE 1S
PRODUCT: gucci
SERIAL: b4949025
TAGS: release-keys
TIME: 1456890097000 2016.03.02 11:41:37:000
TYPE: user
USER: builder
VERSION.CODENAME: REL
VERSION.INCREMENTAL: V7.2.1.0.KHKCNDA
VERSION.RELEASE: 4.4.4
VERSION.SDK_INT: 19
LANG: zh
APP.VERSION.NAME: 1.0
APP.VERSION.CODE: 1
CURRENT: 1460628058178 2016.04.14 18:00:58:178
java.lang.ClassCastException: android.view.ViewGroup$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams
    at android.widget.ListView.measureScrapChild(ListView.java:1183)
    at android.widget.ListView.measureHeightOfChildren(ListView.java:1265)
    at android.widget.ListView.onMeasure(ListView.java:1175)
    at android.view.View.measure(View.java:16525)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
    at android.view.View.measure(View.java:16525)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
    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:16525)
    at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:719)
    at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:455)
    at android.view.View.measure(View.java:16525)
    at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1489)
    at android.view.View.measure(View.java:16525)
    at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:719)
    at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:455)
    at android.view.View.measure(View.java:16525)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
    at android.view.View.measure(View.java:16525)
    at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:719)
    at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:455)
    at android.view.View.measure(View.java:16525)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
    at android.view.View.measure(View.java:16525)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
    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:16525)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291)
    at android.view.View.measure(View.java:16525)
    at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1955)
    at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1152)
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1334)
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1039)
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5648)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
    at android.view.Choreographer.doCallbacks(Choreographer.java:574)
    at android.view.Choreographer.doFrame(Choreographer.java:544)
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
    at android.os.Handler.handleCallback(Handler.java:733)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:136)
    at android.app.ActivityThread.main(ActivityThread.java:5113)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
    at dalvik.system.NativeStart.main(Native Method)

发生了类型转化错误

java.lang.ClassCastException: android.view.ViewGroup$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams

但没有显示发生问题的具体位置,我推测可能是在添加FooterView到ListView是发生问题,原来的代码是

    ViewGroup.LayoutParams params=new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                int p=AppUtil.dip2px(getActivity(),5);
                footerView.setPadding(p,p,p,p);
                footerView.setLayoutParams(params);
                listView.addFooterView(footerView);

后来换成

 ViewGroup.LayoutParams params=new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                int p=AppUtil.dip2px(getActivity(),5);
                footerView.setPadding(p,p,p,p);
                footerView.setLayoutParams(params);
                listView.addFooterView(footerView);

就没有出问题,可能4.4版本对LayoutParameters检查比较严格,没有看4.4的源码。暂且先做记录。

你可能感兴趣的:([工作记录]ListView添加FootView,LayoutParams转化出错)