2012-04-01 84 views
1

我試圖從自定義控件的構造函數中使用自定義屬性的引用來獲取佈局中另一個控件的資源ID。在自定義控件中的getResourceId在Eclipse編輯模式下返回默認值

該控件在運行時非常漂亮,但在編輯器中引發下面顯示的異常。

這段代碼被無恥地從SlideDrawer Android源代碼中篡改,所以我知道它必須工作,而且我做錯了什麼。

自定義控件的構造包含此...

mHandleId = a.getResourceId(R.styleable.Panel_handle, 0); 
    if (mHandleId == 0) { 
     e = new IllegalArgumentException(a.getPositionDescription() + 
       ": The handle attribute is required and must refer to a valid child."); 
    } 

佈局看起來像這樣

<com.xenosoft.hunted.widgets.Panel 
    xmlns:app="http://schemas.android.com/apk/res/com.xenosoft.hunted" 
    android:id="@+id/message_panel" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    app:handle="@+id/message_panel_handle" 
    app:content="@+id/message_panel_content" 
    app:position="left" 
    > 

    <ImageView 
     android:id="@+id/message_panel_handle" 
     android:layout_width="50dp" 
     android:layout_height="fill_parent" 
     android:background="#FF0000"> 

    </ImageView> 

    <LinearLayout 
     android:id="@+id/message_panel_content" 
     android:layout_width="200px" 
     android:layout_height="fill_parent" 
     android:background="#000000"> 
     </LinearLayout> 

</com.xenosoft.hunted.widgets.Panel> 
+0

不知道你是否弄明白了,但是你把這段代碼放在isInEditMode()中嗎? – Shubhayu 2012-07-16 06:40:41

回答

0

你有@+id/message_panel_handle@+id/message_panel_content定義兩次當ID只能被定義一次。參考如何使用RelativeLayout來定位視圖。它應該是:

<com.xenosoft.hunted.widgets.Panel 
    xmlns:app="http://schemas.android.com/apk/res/com.xenosoft.hunted" 
    android:id="@+id/message_panel" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    app:handle="@+id/message_panel_handle" 
    app:content="@+id/message_panel_content" 
    app:position="left" 
    > 

    <ImageView 
    android:id="@id/message_panel_handle" 
    android:layout_width="50dp" 
    android:layout_height="fill_parent" 
    android:background="#FF0000"> 

    </ImageView> 

    <LinearLayout 
    android:id="@id/message_panel_content" 
    android:layout_width="200px" 
    android:layout_height="fill_parent" 
    android:background="#000000"> 
     </LinearLayout> 

</com.xenosoft.hunted.widgets.Panel> 
相關問題