2012-07-06 43 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