在一个 fragment 中,我有一个打开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字段可能不包含在私有(private)类中。”
为什么ButterKnife无法访问私有(private)内部类,但可以自由访问 protected 内部类?
最佳答案
他们不能是私有(private)的,否则它将无法访问它。 ButterKnife
为您生成一些代码,其中包含您不愿意为您编写的所有样板代码。它的作用是,当您编写ButterKnife.bind(this)
时(在这种情况下this
是您的Activity
)尝试通过提供的引用访问每个带有ButterKnife
注释的成员,并使用显式强制转换进行findViewById
。如果成员是私有(private)成员,则不能访问(基本java)。