Android RelativeLayout正在返回LinearLayout布局参数

Android RelativeLayout正在返回LinearLayout布局参数,android,android-linearlayout,android-relativelayout,Android,Android Linearlayout,Android Relativelayout,下面是我的布局xml中的一个片段,它定义了我的RelativeLayout <RelativeLayout android:id="@+id/npvirrButtons" android:layout_width="match_parent" android:layout_height="0dp" > <Button android:id="@+id/buttonNPVLabel" android:layo

下面是我的布局xml中的一个片段,它定义了我的RelativeLayout

<RelativeLayout
    android:id="@+id/npvirrButtons"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    >
    <Button
        android:id="@+id/buttonNPVLabel"
        android:layout_marginLeft="20dp"
        android:layout_alignParentLeft="true"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        />

    <Button
        android:id="@+id/buttonIRR_NFV"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_alignParentRight="true"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        />

</RelativeLayout>
随后,当我尝试将高度从
0dp
更改为
wrap\u content
时,我获取布局参数,如下所示:

RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)this.npvirrButtons.getLayoutParams();
但我抛出了一个异常:

java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.RelativeLayout$LayoutParams
    at com.mydomain.myapp.MyActivity.calculateIRR(MyActivity.java:564)
为什么我的RelativeLayout返回LinearLayout params对象?

getLayoutParams()
根据视图的父对象返回当前布局参数。因此,
按钮nirr\u NFV
将返回
RelativeLayout.LayoutParameter


可以这样想:
getLayoutParams()
是一个方法,您可以调用它来改变当前视图在其容器上的位置。如果要修改其中一个子项的布局方式,则必须获取其布局参数。

请在删除gen&bin文件夹后尝试重新运行项目
java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.RelativeLayout$LayoutParams
    at com.mydomain.myapp.MyActivity.calculateIRR(MyActivity.java:564)