Caused by: Java. Lang. classcastexception: Android. widget. linearlayout $ layoutparams

Source: Internet
Author: User

When switching from: component height, an exception occurs in class conversion as shown in the following figure. I checked it online, as shown below:

These supply parameters toParentOf this view specifying how it shoshould be arranged. There are using subclasses of viewgroup. layoutparams, and these correspond to the different subclasses of viewgroup that are responsible for arranging their children.

So basically, if you are adding a view to another, you must set the layoutparams of the view to the layoutparams type that the parent uses, or you will get a runtime error.

I understand it in this way. If you want to add a view to another layout, you must set the layout parameters of this view to the layout parameter type used by its parent class. That is, to dynamically change the height of a component in the Code, the layout parameter type should be the layout parameter type used by its parent class.

For example:

<Linearlayout

Xmlns: Android = "http://schemas.android.com/apk/res/android"

Android: layout_width = "wrap_content"

Android: layout_height = "wrap_content">

<Framelayout

Android: Id = "@ + ID/framelayout01"

Android: layout_width = "wrap_content"

Android: layout_height = "wrap_content"/>

</Linearlayout>

If you want to dynamically change the size of framelayout in the code, write as follows:

Framelayout = (framelayout) convertview. findviewbyid (R. Id. framelayout01 );
 

Linearlayout. layoutparams FF = new linearlayout. layoutparams (layoutparams. wrap_content, height );

Framelayout. setlayoutparams (ff );

Note: The layout parameter type should be linear layout of linearlayout. layoutparams, rather than framelayout. layoutparams.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.