ClassCastException LinearLayout LayoutParams

被刻印的时光 ゝ 提交于 2019-11-30 12:50:44

What's going on is that a view's LayoutParams are actually the type of the parent. In this case you're getting a FrameLayout$LayoutParams because your LinearLayout is inside a FrameLayout.

Why is it the type of the parent? That's because they store information that's useful for the parent to lay them out. If you were to retrieve the layout parameters for the EditText or ListView you would get LinearLayout$LayoutParams.

Exception clearly states that you are trying to cast FrameLayout to LinearLayout. This occurs in onViewCreated() method in ListSearchFragment.java file, line 184. So replace this:

LayoutParams lp = (LayoutParams) myView.getLayoutParams();

with this:

LinearLayout.LayoutParams lp = 
                  (LinearLayout.LayoutParams) myView.getLayoutParams();

Before you do that, you can hover your mouse pointer on LayoutParams in your current code and see in bubble that it is in fact android.widget.RelativeLayout.LayoutParams hence your exception. See:

There is another reason (or bug) for this exception:

If the view's ID is also being used in another layout, then it'll try to cast LayoutParams for the Parent views used in both layouts.

Solution: Dont use same id in different layouts :)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!