2013-10-21 220 views
0

嗨,大家好我有一个类extends RelativeLayout,我想用RelativeLayout作为父视图组来扩充xml文件,然后将它分配给变量。Android RelativeLayout类抛出异常

public class MyLayout extends RelativeLayout { 
.... 
} 

在活动onCreate()方法:

MyLayout布局;

layout = (MyLayout) inflater.inflate(R.layout.layout_page_one, null); 

这导致了类别转换异常。也许有人可以点亮一些光。

Caused by: java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to com.example.test.MyLayout 
+0

发布您的代码.. – PankajAndroid

+0

在你的layout_page_one.xml中,你是指定一个RelativeLayout还是你的MyLayout? –

回答

1

我们可以强制转换的子类,以超一流的,但不是超级类子CALSS所以在你R.layout.layout_page_one.xml的Xml文件,而不是definig RelativeLayout定义MyLayout thyen它会工作的..