2011-11-13 20 views
0

我遇到了一些非常尷尬的情況。我創建了一個自定義佈局組件,由5個ImageViews的:佈局中包含的組件在運行時沒有標識

select_players_item.xml

<?xml version="1.0" encoding="utf-8"?> 
<org.lzwierko.SelectPlayerItem xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

    <ImageView 
     android:id="@+id/armyImage" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:src="@drawable/army_symbol" /> 

    <TableLayout 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" > 

     <TableRow 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_gravity="right" 
      android:layout_weight=".06" /> 

     <TableRow 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_weight=".40" > 

      <ImageView 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:layout_weight=".4" 
       android:src="@drawable/icon0" 
       android:visibility="invisible" /> 

      <LinearLayout 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:layout_weight=".2" > 
      </LinearLayout> 

      <ImageView 
       android:id="@+id/iconUser" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:layout_weight=".4" 
       android:src="@drawable/icon1" /> 
     </TableRow> 

     <TableRow 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_gravity="right" 
      android:layout_weight=".08" /> 

     <TableRow 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_weight=".4" > 

      <ImageView 
       android:id="@+id/iconInfo" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:layout_weight=".4" 
       android:src="@drawable/icon3" /> 

      <LinearLayout 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:layout_weight=".2" > 
      </LinearLayout> 

      <ImageView 
       android:id="@+id/iconProfileAi" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:layout_weight=".4" 
       android:src="@drawable/icon_4" /> 
     </TableRow> 

     <TableRow 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_gravity="right" 
      android:layout_weight=".06" /> 
    </TableLayout> 

</org.lzwierko.SelectPlayerItem> 

org.lzwierko.SelectPlayerItem類擴展的FrameLayout:

public class SelectPlayerItem extends FrameLayout { 

現在我包括4這個組件在我的其他組件:

<include 
       android:id="@+id/player1" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:layout_gravity="center" 
       android:layout_margin="10dp" 
       android:layout_weight="1" 
       layout="@layout/select_players_item" /> 

現在,你可以看到我的id這包括與

android:id="@+id/player1" 

令人驚訝的是,在運行時,雖然我正確地看到設備上的部件,該

findViewById(R.id.player1) 

返回null。所以我用Hierarchy Viewer進行了檢查,發現這些組件在id中有NO_ID,而不是我在xml文件中設置的那些。

任何人都可以幫助我嗎?我真的需要通過id訪問這些組件。

+0

好的,我發現這個問題,它是在我自己的代碼(顯然)。我重寫了setId()方法,所以視圖構建器(或者其他所謂的)不是設置原始視圖,而是設置其他一些操作。給我打擾;) –

回答

0

您是否試圖獲得對其中有include的佈局的引用,然後使用它獲取player1視圖?就像:

View layout = (View) findViewById(R.id.layout_all); 
layout.findViewById(R.id.player1) 
+0

感謝您的幫助,看我的問題評論。 –