9

在一个片段中,我有一个打开 PopupWindow 的按钮。

private class onMenuClickListener implements View.OnClickListener {
    @BindView(R.id.popup_radiogroup) RadioGroup popupRadioGroup;
    @BindView(R.id.popup_textview) TextView popupTextView;

    PopupWindow popupWindow = getPopupWindow(R.layout.popup_window);

    @Override
    public void onClick(View v) {
        ButterKnife.bind(this, popupWindow.getContentView());
        popupWindow.showAsDropDown(menuButton);
    }
}
private PopupWindow getPopupWindow(int layout_resource_id)  {
    LayoutInflater inflater = (LayoutInflater) getContext()
            .getSystemService(LAYOUT_INFLATER_SERVICE);
    View popupView = inflater.inflate(layout_resource_id,(ViewGroup)getView());

    return new PopupWindow(popupView,
            LinearLayout.LayoutParams.WRAP_CONTENT,
            LinearLayout.LayoutParams.WRAP_CONTENT,true);
}

当我尝试运行此代码时,我收到此错误:“@BindView 字段可能不包含在私有类中。” 为什么 ButterKnife 不能访问私有内部类,却可以自由访问受保护的内部类?

4

1 回答 1

13

他们不能不私有,否则它无法访问它。ButterKnife为您生成一些代码,其中包含您不愿意为您编写的所有样板代码。它的作用是,当您编写ButterKnife.bind(this)时,this在这种情况下是您的Activity,尝试通过ButterKnife您提供的参考访问每个带注释的成员,并findViewById使用显式强制转换。如果成员是私有的,则无法访问(基本 java)。

于 2016-11-04T16:32:00.833 回答