2010-10-21 71 views
0

我在SRC> myproject.test> HomeViewAndroid的自定義視圖強制力接近

自定義視圖在我的主要佈局的xml我有以下幾點:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/home_root" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <myproject.test.HomeView 
     android:id="@+id/home_view" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     > 
    </myproject.test.HomeView> 

</LinearLayout> 

在HomeActivity我有一個電話像onCreate方法一樣。

setContentView(R.layout.main); 
HomeView mHomeView = (HomeView) this.findViewById(R.id.home_view); 

當setContentView方法被調用時,app force關閉。看來,我的主要XML不正確。

任何想法?

+2

什麼是logcat的輸出? – EboMike 2010-10-21 21:38:13

+0

@EBoMiko我在哪裏可以看到? – skyfoot 2010-10-21 22:08:04

+0

從控制檯調用'adb logcat V'。每當你接近一支部隊時,這應該是你的第一本能。 – EboMike 2010-10-21 22:10:48

回答

2

你的意思是它沒有得到對

HomeView mHomeView = (HomeView) this.findViewById(R.id.home_view); 

和崩潰的前行?

檢查你的構造是

HomeView(final Context context, final AttributeSet attrs){ 
    super(context, attrs); 

,而不是

HomeView(final Context context){ 
    super(context); 

你需要的AttributeSet

+0

的輸出是的,如果我註釋掉HomeView安裝線,它仍然強制關閉 – skyfoot 2010-10-22 07:48:53

+0

您如何初始化您的主視圖?你能顯示一些代碼,並從logcat的輸出 – GaryD 2010-10-22 08:08:19

+0

剛剛出來,你的佈局也稱爲main.xml?它是在佈局目錄? – GaryD 2010-10-22 13:51:56

0

檢查構造函數,你HomeView實現:

public HomeView(Context context, AttributeSet atts) { 
    super(context, atts); 
} 
0
<LinearLayout xmlns:android = 
     http://schemas.android.com/apk/res/android" 
    android:id="@+id/home_root" 
    android:orientation="vertical" 

沒有我的佈局有@+id。也許你應該將視圖設置爲home_root。請與您R.java的佈局的名稱,或嘗試

setContentView(R.layout.home_root);