将RelativeLayout转换为RelativeLayout派生类时出错
问题描述:
虽然我的类派生自RelativeLayout,但我得到一个运行时错误,指出无法将RelativeLayout转换为我的类。将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"
答
那是因为那不是什么对象。如果你想要像你需要把你的类在你的布局像这样
<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>
如果我作出这样的改变使用你的类我得到一个无法加载错误,由于未找到“ClassNotFoundException的类:没” t找到类“ripples.company.com.ripples.RipplesBackground”。为什么? – Gruntcakes
确保你把路径不正确,并没有拼错任何东西 – tyczj