ranys ranys - 1 year ago 130
Android Question

Why can't ButterKnife bind fields that are in private inner classes?

In a fragment, I have a button that opens up a 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);

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

return new PopupWindow(popupView,

When I try to run this code, I get this error: "@BindView fields may not be contained in private classes."
How come ButterKnife can't access private inner classes, but it can freely access protected ones?

Answer Source

they can't not be private because otherwise it could not access it. ButterKnife generates some code for you that contains all the boilerplate code you are not willing to write for you. What it does, when you write ButterKnife.bind(this), where this in this case is your Activity, is trying to access every ButterKnife annotated member trough the reference you provided, and do a findViewById with the explicit cast. If the member is private it can't be access (basic java).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download