gpt4 book ai didi

android - 为什么 ButterKnife 不能绑定(bind)私有(private)内部类中的字段?

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:03:13 25 4
gpt4 key购买 nike

在 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)。

关于android - 为什么 ButterKnife 不能绑定(bind)私有(private)内部类中的字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40427422/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com