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

Java 如何在Android中从充气机获取ImageView ID,java,android,xml,Java,Android,Xml,我用一个充气机来显示一个由我制作的弹出式设置菜单。它由一些图像和按钮组成。完成XML布局后,我开始对其进行编码,它将使用以下代码正确打开: public void Settings_button(View view) { if (p != null) showPopup(Main_activity.this, p); } @Override public void onWindowFocusChanged(bool

我用一个充气机来显示一个由我制作的弹出式设置菜单。它由一些图像和按钮组成。完成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);

    }
但是现在我需要获得ImageView 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\u setting\u 1\u icon.setImageResource(R.id.setting\u done\u icon)我得到应用程序崩溃与
NullPointerException

我读过类似于应该从充气机中获取指针的内容,但我尝试在
showPopup
方法中这样做,但什么都没有。如何解决这个问题?

我遇到了一个非常类似的问题,即以编程方式膨胀容器并填充它。这就是我的工作原理:

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

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

我遇到了一个非常类似的问题,即以编程方式膨胀容器并填充它。这就是我的工作原理:

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

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

在显示弹出式方法中初始化视图:

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

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

您的弹出式布局是否包含此图像“弹出式设置图标”?显示您正在使用的代码
popup\u设置图标。setImageResource(R.id.setting\u done\u图标)行您声明的
popup\u设置\u 1\u图标
既是按钮又是图像视图,这其中之一是打字错误吗?或者只是展示你的尝试?不管怎样,如果findViewById在中找不到id,它将返回nulllayout@justDroid我已经检查了多次,看它是否是一个类型错误,但什么都没有。@cricket\u 007这是一个粘贴错误,我现在已经更正了。您的弹出式布局是否包含此图像“弹出式设置”图标?显示您正在使用的代码
popup\u设置\u图标。setImageResource(R.id.setting_done_图标)
line您声明的
popup\u setting\u 1\u图标既是按钮又是图像视图,这是打字错误吗?还是仅仅显示您尝试的内容?如果findViewById在layout@justDroid我已经检查了多次,看是否是一个类型错误,但什么都没有。@cricket\u 007这是一个粘贴错误,我已经更正了ed now忘记将其添加到最后一行,现在应该可以工作了。我认为它也可能用于充气方法而不是null忘记将其添加到最后一行,现在应该可以工作了。我认为它也可能用于充气方法而不是null