ButterKnife binding views with inheritance issue

蓝咒 提交于 2019-12-07 05:32:17

问题


I have already read Some issues on github, but still haven't found solution.

My problem is that I cannot bind views in a child activity inherited from BaseActivty

Code in my BaseActivity

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ViewGroup rootView = (ViewGroup) this.findViewById(android.R.id.content);
        View rootBaseView = getLayoutInflater().inflate(R.layout.activity_base, rootView, false);
        ButterKnife.bind(this, rootBaseView);
        ....
         int childLayoutID = getLayoutResId();
        if (childLayoutID != LayoutConstants.NULL_LAYOUT_ID) {
           View childRootView = getLayoutInflater().inflate(childLayoutID, mLinearLayoutFrameContainer, false);
           mLinearLayoutFrameContainer.addView(childRootView);
       }
        ....
        setContentView(rootBaseView);

So, I have abstract method

   public abstract
    @LayoutRes
    int getLayoutResId();

In any child activity I return specific layout resource.

All views in BaseActivity are injected correctly, but in any child activity views cannot be injected even with @Nullable annotation, view is null in runtime.

How can I solve this problem. maybe there are some workarounds ?

ADDITIONAL INFO

I have tried different ways like

    ButterKnife.bind(this);
    ButterKnife.bind(this,mLinearLayoutFrameContainer);

In child activity in onCreate method.

But still the same

But with fragment I have the same situation and it works.

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    ButterKnife.bind(this, view);
    Log.e("FRAGMENT", "ON VIEW CREATED " + getClass().getCanonicalName());

And in any child fragment I don't call ButterKnife inject methods at all, fields just annotated and of course I start using views only in onViewCreated after super call.

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    setupViews();

来源:https://stackoverflow.com/questions/35910037/butterknife-binding-views-with-inheritance-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!