【发布时间】:2015-08-31 14:39:55
【问题描述】:
大家好,我正在尝试在我的应用程序中显示一条自定义消息,但是在将填充设置为程序化布局时,我不断收到此错误:
Fatal Exception:java.lang.ClassCastExceptionandroid.widget.RelativeLayout$LayoutParams cannot be cast to android.widget.FrameLayout$LayoutParams
这是我尝试设置填充的方法:
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
if(alignTop){
params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
int actionBarHeight = 0;
TypedValue tv = new TypedValue();
if (getContext().getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true))
{
actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data,getContext().getResources().getDisplayMetrics());
}
params.setMargins(0, actionBarHeight, 0,0);
}else{
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
}
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
params.addRule(RelativeLayout.ALIGN_PARENT_START);
MessageView.this.setLayoutParams(params);
这是我的 XML 文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:roboto="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#1e1e1e"
android:id="@+id/messageViewBackground"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:padding="10dp">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:id="@+id/messageViewSpinnerLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp">
<ImageView
android:id="@+id/messageViewSpinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/icon_spinner_small"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/icon_spinner_logo_small"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
<ImageView
android:id="@+id/messageViewIcon"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:src="@android:drawable/stat_sys_warning"
android:visibility="gone"/>
<com.myapp.widgets.RobotoText
android:id="@+id/messageViewText"
roboto:font="Light"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#5B5B5B"
android:layout_gravity="center_vertical" />
</LinearLayout>
</RelativeLayout>
我做错了什么?
【问题讨论】:
-
如果是 RelativeLayout.LayoutParams,请尝试设置 FrameLayout.LayoutParams。在这个类似的问题中:stackoverflow.com/questions/11544964/…
-
如果您查看我的代码,您会发现我正在做与您的建议类似的事情,但它不起作用。
-
不,您仍在使用 RelativeLayout.LayoutParms 而不是 FrameLayout.LayoutParams。你看答案了吗?
-
是的,我的错误改变了它,但是现在消息设置在屏幕的顶部,我不能将它设置在底部
-
另一种选择是关注这个问题(你有没有试过用谷歌搜索你的问题?)stackoverflow.com/questions/19604891/…
标签: java android exception classcastexception