0

我有一个菜单布局,可以在更多活动中重复使用更多次......菜单布局称为menu_up_row.xml,布局的每个按钮都称为menu_up_cell.xml。因此,我以编程方式为每个 Activity 膨胀 menu_up_row.xml,同时,menu_up_cell.xml 也膨胀了菜单有多少按钮。

此异常大约每 70 个用户发生一次……这是对引发异常的名为 MenuUp 的类的静态方法的调用:

    public static void *etMenuUpButtonEnabled(Context ctx, LinearLayout menuUpLayout, int buttonID, boolean enabled) {
            LinearLayout btnLayout = (LinearLayout) menuUpLayout.findViewById(getResourceId(buttonID));
            //SOMETIMES NULL POINTER EXCEPTION HERE:
            ImageView menuCellImageView =(ImageView) btnLayout.findViewById(R.id.menuCellImageView); //<-- NULL POINTER EXCEPTION HERE
            ...

在哪里 :

  private static int getResourceId(int buttonID) {
    int retVal = 0;
    switch (buttonID){
    case BUTTON_MENU_UP_BTN1:
        retVal = R.id.menuCell1;
        break;
    case BUTTON_MENU_UP_BTN2:
        retVal = R.id.menuCell2;
        break;
    case BUTTON_MENU_UP_BTN3:
        retVal = R.id.menuCell3;
    }     

    return retVal;
}

上面的方法是从 Main Activity 调用的

...
LinearLayout menuUpLayout = (LinearLayout) findViewById(R.id.main_menu_up_row);
MenuUp.setMenuUpButtonEnabled(ctx, menuUpLayout, MenuUp.BUTTON_MENU_UP_BTN1,false);
...

Main Activity 的 xml 中有这个用于包含菜单:

<LinearLayout android:id="@+id/main_menu_up_row"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="50sp"
    android:visibility = "gone"
    >
</LinearLayout>

要使菜单膨胀,在主活动中我有以下代码:

MenuUp.inflateMenuUpCustom(ctx, menuUpLayout);

在哪里:

public static void inflateMenuUpCustom(Context ctx, LinearLayout menuUpLayout) {
    LayoutInflater layoutInflater = (LayoutInflater) 
            ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);    
    menuUpLayout.addView(layoutInflater.inflate(R.layout.menu_up_row, menuUpLayout, false) );

    //Aggiungo i bottoni del menu
    addMenuUpButton(ctx, menuUpLayout, R.id.menuCell1, R.drawable.menu_grid_order_by, R.string.menu_grid_order_by_str);
    addMenuUpButton(ctx, menuUpLayout, R.id.menuCell2, R.drawable.menu_grid_add, R.string.menu_grid_add_str);
    addMenuUpButton(ctx, menuUpLayout, R.id.menuCell3, R.drawable.menu_grid_filter_by, R.string.menu_grid_filter_by_str);

    }

和:

private static void addMenuUpButton(final Context ctx
                                  , LinearLayout menuUpLayout
                                  , int cellResourceId
                                  , int imageDrawableId
                                  , Integer stringResourceId) {
    LayoutInflater layoutInflater = (LayoutInflater) 
            ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);    
    LinearLayout btnLayout = (LinearLayout) menuUpLayout.findViewById(cellResourceId);
    btnLayout.addView(layoutInflater.inflate(R.layout.menu_up_cell, btnLayout, false) );
    ImageView menuCellImageView =(ImageView) btnLayout.findViewById(R.id.menuCellImageView);
    LinearLayout menuCellLinearLayout =  (LinearLayout) btnLayout.findViewById(R.id.menuCellLinearLayout);
    menuCellImageView.setImageDrawable(ctx.getResources().getDrawable(imageDrawableId));
    ...

这是menu_up_row.xml:

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


    <LinearLayout android:id="@+id/radRowToBeSeen"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >

        <LinearLayout android:id="@+id/linearLayoutDets4"
             android:orientation="horizontal" 
             android:layout_width="fill_parent"
             android:layout_height="fill_parent"
             android:background="#e7e7e7"
         >

                <LinearLayout android:id="@+id/menuCellCont1"
                    android:layout_weight="0.2" 
                    android:layout_width="0dip"
                    android:layout_height="fill_parent"
                    android:orientation="horizontal" >
                    <LinearLayout android:id="@+id/menuCell1"
                        android:orientation="vertical"
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent">
                    </LinearLayout>                        
                </LinearLayout>
                <LinearLayout android:id="@+id/menuCellCont2"
                    android:layout_weight="0.2" 
                    android:layout_width="0dip"
                    android:layout_height="fill_parent"
                    android:orientation="horizontal" >
                    <LinearLayout android:id="@+id/menuCell2"
                        android:orientation="vertical"
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent">
                    </LinearLayout>                        
                </LinearLayout>
                <LinearLayout android:id="@+id/menuCellCont3"
                    android:layout_weight="0.2" 
                    android:layout_width="0dip"
                    android:layout_height="fill_parent"
                    android:orientation="horizontal" >
                    <LinearLayout android:id="@+id/menuCell3"
                        android:orientation="vertical"
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent">
                    </LinearLayout>                        
                </LinearLayout>
            </LinearLayout>


    </LinearLayout>
</LinearLayout> 

这是menu_up_cell.xml

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

    <LinearLayout 
        android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        >

        <ImageView
            android:id="@+id/menuCellImageView"
            android:layout_gravity="center_vertical"
            android:adjustViewBounds="true"
            android:scaleType="centerInside"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:padding="2dp"
            android:src="@drawable/menu_dummy" 
            android:visibility="gone"
            >
        </ImageView>
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:orientation="horizontal"
            >

            <TextView
                android:id="@+id/menuCellTextView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:maxLines="2"
                android:text="ciao"
                android:textColor="#ffffff"
                android:textSize="13sp" />

         </LinearLayout>

    </LinearLayout>
</LinearLayout>

我错了什么?

4

0 回答 0