2011-05-19 74 views
1

在我的應用程序中,我有一個導航菜單,它在活動之間是不變的。爲了使我的代碼更容易修改,我將它製作成了它自己的xml,並且已經爲我的每個屏幕導入了我的佈局。從XML中獲取視圖

要設置按鈕我做了一個類,我通過我當前的活動。新類沒有問題找到按鈕的視圖,但找不到菜單的封裝佈局,並在嘗試findViewById()時返回null。由於按鍵和佈局在同一個XML

public static void setup(Activity a){ 
    myActivity = a; 

    //OFFENDING BIT OF CODE 
    View myLayout = (View) myActivity.findViewById(R.layout.navigation_bar); 

    Log.d(TAG, "layout: "+myLayout); 

    set_btn = (ImageButton) myActivity.findViewById(R.id.a_settings); 
    set_btn.setPressed(false); 
    set_btn.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      //TODO: 
     } 
    }); 

    inbox_btn = (ImageButton) myActivity.findViewById(R.id.a_offers); 
    inbox_btn.setPressed(false); 
    inbox_btn.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      //TODO: 
     } 
    }); 

} 

主屏XML

<RelativeLayout android:layout_height="wrap_content" 
    android:layout_width="wrap_content" 
    android:id="@+id/navigationBar" 
    android:layout_alignParentBottom="true"> 
    <include android:layout_height="wrap_content" 
     android:id="@+id/include1" 
     layout="@layout/navigation_bar" 
     android:layout_width="wrap_content"></include> 
</RelativeLayout> 

菜單XML

<RelativeLayout mlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:id="@+id/NavigationLayout"> 

    <RelativeLayout android:id="@+id/buttonLayout" 
     android:layout_height="75dip" 
     android:layout_width="fill_parent" 
     android:layout_alignParentBottom="true"> 

     <ImageButton android:id="@+id/a_settings" 
      android:layout_width="100dip" 
      android:background="@drawable/settings_button" 
      android:layout_centerHorizontal="true" 
      android:layout_centerVertical="true" 
      android:layout_height="fill_parent" 
      android:scaleType="fitXY"> 
     </ImageButton> 

     <ImageButton android:id="@+id/a_wallet" 
      android:layout_width="100dip" 
      android:background="@drawable/wallet_button" 
      android:layout_toLeftOf="@+id/a_settings" 
      android:layout_alignBottom="@+id/a_settings" 
      android:layout_height="fill_parent" 
      android:scaleType="fitXY"> 
     </ImageButton> 

     <ImageButton android:id="@+id/a_offers" 
      android:layout_width="100dip" 
      android:background="@drawable/offers_button" 
      android:layout_toRightOf="@+id/a_settings" 
      android:layout_alignBottom="@+id/a_settings" 
      android:layout_height="fill_parent" 
      android:scaleType="fitXY"> 
     </ImageButton> 

    </RelativeLayout> 

</RelativeLayout> 

回答

2

即使你的菜單佈局確實叫navigation_bar,你給你的包括菜單佈局的include1的ID - >android:id="@+id/include1"。如果你想在你的活動中解決它(或者試圖找到ImageButtons,它們也可見),請嘗試findViewById(R.id.include1)

1

我不是100%,但我會去上肢體並說你可能需要從你的myLayout視圖中調用findViewById()(它也可能存在轉換問題)。

所以這個代替:

public static void setup(Activity a){ 
    myActivity = a; 

    //OFFENDING BIT OF CODE 
    myLayout = (View) myActivity.findViewById(R.layout.navigation_bar); 

    Log.d(TAG, "layout: "+myLayout); 

    set_btn = (ImageButton) myLayout.findViewById(R.id.a_settings); 
    set_btn.setPressed(false); 
    set_btn.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      //TODO: 
     } 
    }); 

    inbox_btn = (ImageButton) myLayout.findViewById(R.id.a_offers); 
    inbox_btn.setPressed(false); 
    inbox_btn.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      //TODO: 
     } 
    }); 

}