问题
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