3
在一个片段中,我有一个打开PopupWindow的按钮。ButterKnife为什么不能绑定私有内部类中的字段?
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如何不能访问私人内部类,但它可以自由访问受保护的内部类?
'protected'启用包可见性。看看[这里](https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html) – Blackbelt
主要原因还在于它依赖于注释而不是纯粹的反射(它可以*做的地方一些魔术),但它会臃肿和更复杂,因为它需要桥接访问。将它们标记为可见是没有错的,事实上,我认为它不应该受到保护,它们应该什么也没有(default = package)。 –