将RelativeLayout强制转换为RelativeLayout派生类时出错

时间:2015-07-20 19:37:08

标签: android android-layout

我收到运行时错误,说不能将RelativeLayout强制转换为我的类,即使我的类派生自RelativeLayout。

这是我的班级:

public class RippleBackground extends RelativeLayout{
   ....

这是发生崩溃的主要活动中的一行:

 final RippleBackground rippleBackground=(RippleBackground)findViewById(R.id.embeddedContent);

这是布局内容:

<RelativeLayout
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/textView"
    android:layout_alignParentStart="true"
    android:id="@+id/embeddedContent"
    app:rb_color="#0099CC"
    app:rb_radius="32dp"
    app:rb_rippleAmount="4"
    app:rb_duration="3000"
    app:rb_scale="6"
    android:layout_marginTop="71dp"></RelativeLayout>

错误是:

"android.widget.RelativeLayout cannot be cast to RippleBackground"

1 个答案:

答案 0 :(得分:1)

那是因为那不是对象是什么。如果你想像你那样使用你的课程,你需要把你的课程放在你的布局中

<com.mypackage.RippleBackground
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/textView"
android:layout_alignParentStart="true"
android:id="@+id/embeddedContent"
app:rb_color="#0099CC"
app:rb_radius="32dp"
app:rb_rippleAmount="4"
app:rb_duration="3000"
app:rb_scale="6"
android:layout_marginTop="71dp"></com.mypackage.RippleBackground>
相关问题