9

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

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

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?

ranys
  • 256
  • 2
  • 9

1 Answers1

13

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

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
  • 2
    `protected` enables package visibility. Have a look [here](https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html) – Blackbelt Nov 04 '16 at 16:38
  • 2
    The main reason is also that it relies on annotations not pure reflection (where it *could* do some magic) but then it would be bloated and more complicated for it would need to bridge the access. There's nothing wrong in marking them package visible, in fact, I'd argue that instead of protected, they should have nothing (default = package). – Martin Marconcini Nov 04 '16 at 18:26
  • hi, why should the method not be static? – Siva Nov 25 '19 at 00:46