java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams
看下如下错误提示:
java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams
at android.widget.GridView.onMeasure(GridView.java:1042)
at com.sjk.md.adapter.NoScrollGridView.onMeasure(NoScrollGridView.java:25)
at android.view.View.measure(View.java:15604)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4918)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1411)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:698)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:15604)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4918)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:15604)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4918)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1411)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:698)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:15604)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4918)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2222)
at android.view.View.measure(View.java:15604)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2165)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1249)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1443)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1139)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4879)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:776)
at android.view.Choreographer.doCallbacks(Choreographer.java:579)
at android.view.Choreographer.doFrame(Choreographer.java:548)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:762)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:153)
at android.app.ActivityThread.main(ActivityThread.java:5297)
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:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
这个问题困扰了我很久, 错误提示是在Gridview ndroid.widget.GridView.onMeasure(GridView.java:1042)
一看就懵逼了,错误在jar 文件中,没法啊,,
1. 我发现在手机 apk 19+ 以上手机都不会出现这个问题,,但是换成 4.2 手机就会出现,,当时就想会不会api 17 太低了导致,更改build.gradle 文件仍然不生效。
2. 同时又去发现 以前项目发在eclipse 上同样代码,运行在 4.2 上可以执行, 但是用android studio后就是出现这个错误。 我想会不会jar 文件不同导致的呢(eclipse和as 我用的不同sdk),, 这时候我又互换了sdk,,下载更新,测了一下午。结果没成功。
3. 我差点要放弃了,百度搜下,发现很多类似的错误都是出现在代码上,我决定在 android studio写个demo ,gridview 运行竟然可以,我就怀疑,应该代码自己写的问题了。
开始仔细检查,同时继续分析这个 错误异常log,,,
.measureChildBeforeLayout(LinearLayout.java:1411)
我在想肯定 gridview onmeasure 方法执行时候相关有Linearlayout ,,找代码相关布局,,结果发现问题所在,,一下贴出错误代码
activity 布局xml
<LinearLayout这个应该没问题
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="@color/text_white"
android:layout_marginTop="20dp">
<GridView
android:id="@+id/reg_Grid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:gravity="center"
android:horizontalSpacing="10dp"
android:numColumns="2"
android:listSelector="#00000000"
android:scrollbars="none"
android:verticalSpacing="10dp" >
</GridView>
</LinearLayout>
然后检查 getView 了,,看看
@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
View view = LayoutInflater.from(Activity_sendcertificate.this).inflate(R.layout
.itemview_photo, null);
ImageView im = (ImageView) view.findViewById(R.id.img_photo);
if (imgList[arg0] == null) {
im.setImageDrawable(res.getDrawable(dRes[arg0]));
} else {
im.setImageBitmap(Util
.getSmallBitmap(imgList2[arg0], 50, 50));
}
return im
}
结果发现,,return im ,,, 坑大了,,,这个是以前复制别人代码的,自己没发现埋下了坑。
改成 return view 就可以了。
以上是我遇到问题经历,还是没有好好分析log,, 胡乱猜测问题,自己在蹲坑蹲了很久,差点虚脱了,,希望小伙伴们不要跳坑了
谨记代码复制别人的一定要谨慎使用,尽量少复制,,遇到问题结合问题代码分析,都方位测试分析,有助于找到问题所在。。