繁体   English   中英

如何从Android中的充气机获取ImageView ID

[英]How to get ImageView ID from inflater in Android

我正在使用充气机显示由我制作的弹出设置菜单。 它由一些图像和按钮组成。 完成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和Buttons 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导致应用程序崩溃。 我读过一些东西,例如我应该从充气机获取指针,但是我尝试在showPopup方法中这样做,但是什么也没有。 我该如何解决这个问题?

用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);

在以编程方式为容器充气并填充容器时,我遇到了一个非常相似的问题。 这对我有用:

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);

这是假设您的设置按钮和图像视图在设置弹出式布局内。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM