简体   繁体   English

无法将Java.lang.classcastexception linearlayout.layoutParams强制转换为framelayout.layoutparams

[英]Java.lang.classcastexception linearlayout.layoutParams cannot be cast to framelayout.layoutparams

This is XML code, 这是XML代码,

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swipe"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/light_grey" >

        <ScrollView
            android:id="@+id/scrollView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:scrollbars="none" >

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:paddingBottom="@dimen/activity_vertical_margin"
                android:paddingLeft="@dimen/activity_horizontal_margin"
                android:paddingRight="@dimen/activity_horizontal_margin"
                android:paddingTop="@dimen/activity_vertical_margin" >

                <RelativeLayout
                    android:id="@+id/logolayout"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@drawable/layout_bg" >

                    <ImageView
                        android:id="@+id/company_logo"
                        android:layout_width="80dp"
                        android:layout_height="80dp"
                        android:layout_alignParentTop="true"
                        android:layout_marginLeft="5dp"
                        android:layout_marginTop="5dp"
                        android:background="@drawable/iv_bg"
                        android:scaleType="centerInside" />

                    <TextView
                        android:id="@+id/company_name"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="10dp"
                        android:layout_marginTop="8dp"
                        android:layout_toRightOf="@+id/company_logo"
                        android:gravity="center"
                        android:text="The D Corporation Inc."
                        android:textSize="18dp"
                        android:textStyle="bold" />

                    <TextView
                        android:id="@+id/sss_name"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:layout_below="@+id/company_name"
                        android:layout_marginLeft="10dp"
                        android:layout_marginTop="1dp"
                        android:layout_toRightOf="@+id/company_logo"
                        android:gravity="center"
                        android:text="RD Sales Management"
                        android:textSize="16dp"
                        android:textStyle="bold" />

                    <TextView
                        android:id="@+id/login_user_name"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:layout_below="@+id/sss_name"
                        android:layout_marginLeft="10dp"
                        android:layout_marginTop="1dp"
                        android:layout_toRightOf="@+id/company_logo"
                        android:gravity="center"
                        android:singleLine="true"
                        android:textColor="#E55451"
                        android:textSize="15dp"
                        android:textStyle="bold" />
                </RelativeLayout>

                <RelativeLayout
                    android:id="@+id/workorderlayout"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/logolayout"
                    android:layout_marginTop="8dp"
                    android:background="@drawable/layout_bg" >

                    <TextView
                        android:id="@+id/header_text"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="10dp"
                        android:layout_marginTop="12dp"
                        android:gravity="left"
                        android:text="Today’s Activity"
                        android:textSize="17dp"
                        android:textStyle="bold" />

                    <Button
                        android:id="@+id/id_linesp"
                        android:layout_width="match_parent"
                        android:layout_height="1dp"
                        android:layout_below="@+id/header_text"
                        android:layout_marginTop="8dp"
                        android:background="@color/light_grey" />

                    <HorizontalScrollView
                        android:layout_below="@+id/id_linesp"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content" >

                        <LinearLayout
                            android:id="@+id/lylat"
                            android:layout_width="fill_parent"
                            android:layout_height="wrap_content"
                            android:layout_marginTop="10dp"
                            android:orientation="horizontal"
                            android:paddingLeft="2dp"
                            android:paddingRight="2dp"
                            android:weightSum="3" >

                            <TableLayout
                                android:id="@+id/invoice_st"
                                android:layout_width="0dp"
                                android:layout_height="88dp"
                                android:layout_marginRight="5dp"
                                android:layout_weight="1"
                                android:background="@drawable/btn_bg"
                                android:paddingBottom="6dip" >

                                <TableRow>

....... still code flows .......仍然是代码流

I have used following piece of code in class to set the width of Linear layout present inside Horizontall scrollview , 我在类中使用了以下代码来设置Horizo​​ntall scrollview中存在的Linear布局的宽度,

mLinearLayout = (LinearLayout) findViewById(R.id.lylat);

        DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);


        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(metrics.widthPixels/4, HorizontalScrollView.LayoutParams.MATCH_PARENT);
        mLinearLayout.setLayoutParams(params);

app crashes while setting the width here HorizontalScrollView.LayoutParams.MATCH_PARENT 此处设置宽度时,应用程序崩溃HorizontalScrollView.LayoutParams.MATCH_PARENT

and it says java.lang.classcastexception linearLayout.layoutparams cannot be cast to framelayout.layoutparams ,, your help will be very much thankful 它说java.lang.classcastexception linearLayout.layoutparams无法转换为framelayout.layoutparams,您的帮助将非常感谢

The type of layout parameters that a view can be assigned does not depend on the class of the view, but rather on the class of its container. 可以分配视图的布局参数的类型不取决于视图的类,而是取决于其容器的类。 In this case, the container is a HorizontalScrollView , which requires a FrameLayout.LayoutParameters for the child. 在这种情况下,容器是HorizontalScrollView ,它需要子级的FrameLayout.LayoutParameters If all you want to do is set the width and height, do this: 如果您只想设置宽度和高度,请执行以下操作:

ViewGroup.LayoutParams params = mLinearLayout.getParams();
params.layout_width = metrics.widthPixels/4;
params.layout_height = ViewGroup.LayoutParams.MATCH_PARENT;
mLinearLayout.setLayoutParams(params);

In other words, use the LayoutParams that were already assigned to mLinearLayout by its parent. 换句话说,使用其父级已经分配给mLinearLayoutLayoutParams

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.lang.ClassCastException:android.widget.LinearLayout $ LayoutParams无法转换为android.widget.FrameLayout - java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.FrameLayout java.lang.ClassCastException:android.widget.RelativeLayout $ LayoutParams无法转换为android.widget.FrameLayout $ LayoutParams - java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams cannot be cast to android.widget.FrameLayout$LayoutParams java.lang.ClassCastException:android.widget.RelativeLayout $ LayoutParams无法转换为android.widget.LinearLayout $ LayoutParams - java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams cannot be cast to android.widget.LinearLayout$LayoutParams java.lang.ClassCastException:android.widget.LinearLayout $ LayoutParams无法转换为android.widget.AbsListView $ LayoutParams - java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams Android java.lang.ClassCastException:android.widget.LinearLayout $ LayoutParams无法强制转换为android.widget.AbsListView $ LayoutParams - Android java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams 错误:java.lang.ClassCastException LinearLayout$LayoutParams cannot be cast, when starting app - Error: java.lang.ClassCastException LinearLayout$LayoutParams cannot be cast ,when starting app java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams 不能转换为 android.support.constraint.ConstraintLayout$LayoutParams - java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.support.constraint.ConstraintLayout$LayoutParams java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams 不能转换为 android.support.v7.widget.RecyclerView$LayoutParams - java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.support.v7.widget.RecyclerView$LayoutParams java.lang.ClassCastException:GridLayout.LayoutParams无法强制转换为Frame.LayoutParams - java.lang.ClassCastException: GridLayout.LayoutParams cannot cast to Frame.LayoutParams ClassCastException:java.lang.ClassCastException:android.widget.LinearLayout $ LayoutParams无法强制转换为android.support.v4.widget.DrawerLayout - ClassCastException : java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.support.v4.widget.DrawerLayout
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM