我得到ClassCastException:android.widget.LinearLayout 我试图抓住视图(Lineralayout)中包含listview(其中一个是孩子)元素的事实。
customview:
public class MasterView extends View {
public boolean done=false;
public MasterView(Context context) {
super(context);
}
public MasterView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MasterView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onFinishInflate() {
// TODO Auto-generated method stub
done=true;
super.onFinishInflate();
}
}
我称之为:
View master = (View)findViewById(R.id.master);
MasterView master2 = (MasterView)master; //exception
xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/master"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<RelativeLayout
android:id="@+id/cnt"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.03" >
</RelativeLayout>
</LinearLayout>
我用列表
来膨胀cnt答案 0 :(得分:3)
xml中的R.id.master元素不是MasterView类型。将您的xml更改为:
<yourpackage.MasterView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/master"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<RelativeLayout
android:id="@+id/cnt"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.03" >
</RelativeLayout>
</yourpackage.MasterView>