2012-07-06 45 views
1

我得到ClassCastException异常︰android.widget.LinearLayout 我试图捕捉的事实,视图(Lineralayout)具有listview(它的一个孩子)的元素,在里面。Android转换为自定义ViewClassException:android.widget.LinearLayout

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 

<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与列表中的XML

+0

显示XML代码也.. – 2012-07-06 18:37:28

+0

R.id.master是类型的LinearLayout不马西德威 – 2012-07-06 18:58:54

回答

2

的R.id.你的xml中的主元素不是o f类型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> 
+0

延伸视图崩溃的应用程序,我不得不延长形成的LinearLayout。 – user1317422 2012-07-09 08:39:31