如何从Android中的inflater获取ImageView ID

时间:2016-02-03 12:48:55

标签: java android xml

我正在使用inflater来显示我制作的弹出设置菜单。它由一些图像和按钮组成。完成XML布局后,我开始对其进行编码,使用以下代码正确打开:

    public void Settings_button(View view)
    {
        if (p != null)
            showPopup(Main_activity.this, p);
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {

        int[] location = new int[2];
        ImageButton setting_button = (ImageButton) findViewById(R.id.settings_button);

        // Get the x, y location and store it in the location[] array
        // location[0] = x, location[1] = y.
        setting_button.getLocationOnScreen(location);

        //Initialize the Point with x, and y positions
        p = new Point();
        p.x = location[0];
        p.y = location[1];
    }

    // The method that displays the popup.
    private void showPopup(final Activity context, Point p) {
        Resources r = getResources();
        int popupWidth = 500;
        int popupHeight = 300;

        // Inflate the popup_layout.xml
        LinearLayout viewGroup = (LinearLayout) context.findViewById(R.id.popup);
        LayoutInflater layoutInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View layout = layoutInflater.inflate(R.layout.settings_popup_layout, viewGroup);

        // Creating the PopupWindow
        final PopupWindow popup = new PopupWindow(context);
        popup.setContentView(layout);
        popup.setWidth(popupWidth);
        popup.setHeight(popupHeight);
        popup.setFocusable(true);

        // Some offset to align the popup a bit to the right, and a bit down, relative to button's position.
        int OFFSET_X = 150;
        int OFFSET_Y = 30;

        // Displaying the popup at the specified location, + offsets.
        popup.showAtLocation(layout, Gravity.NO_GRAVITY, p.x + OFFSET_X, p.y + OFFSET_Y);

    }

但现在我需要获取 ImageViews ID和按钮 ID,我用过:

//In onCreate of Main_Activity
ImageView popup_setting_1_icon;
popup_setting_1_icon = (ImageView) findViewById(R.id.setting_1_icon);
Button popup_setting_1_button;
popup_setting_1_button = (Button) findViewById(R.id.setting_1_button);

但是,当我开始使用此视图作为示例popup_setting_1_icon.setImageResource(R.id.setting_done_icon);时,我的应用程序崩溃NullPointerException。 我读过类似于我应该从inflater获取指针的内容,但我试图在showPopup方法中这样做,但没有。我怎么能解决这个问题?

2 个答案:

答案 0 :(得分:2)

在show popup方法中初始化视图:

ImageView popup_setting_1_icon;
popup_setting_1_icon = (ImageView)layout. findViewById(R.id.setting_1_icon);
Button popup_setting_1_button;
popup_setting_1_button = (Button)layout. findViewById(R.id.setting_1_button);

答案 1 :(得分:0)

我有一个非常类似的问题,以编程方式为容器充气并填充它。这对我有用:

LayoutInflater inflator = LayoutInflater.from(getContext()); 
LinearLayout viewGroup = (LinearLayout) getActivity().findViewById( 
    R.id.popup); 
View layout = inflator.inflate(R.layout.settings_popup_layout, null);
Button button = (Button) layout.findViewById(R.id.setting_1_button);
ImageView image = (ImageView) layout.findViewById(R.id.setting_1_icon);
//bind data to view/buttons
viewGroup.addView(layout);

这是假设您的设置按钮和图像视图位于设置弹出布局中。