我想使用Butterknife,但也要保持Drawer逻辑在BaseActivityClass中创建和维护,并由我所有其他具有不同layoutIds的活动扩展。
尝试通过抽象方法提供LayoutId并手动膨胀,但Butterknife错误地错过了:
Caused by: java.lang.IllegalStateException: Required view 'View' with ID 2131493000 for field 'mView' was not found. If this view is optional add '@Nullable' annotation.
基类
@Bind(R.id.leftDrawer)
protected ListView mLeftDrawer;
@Bind(R.id.drawerLayout)
protected DrawerLayout mDrawerLayout;
@Bind(R.id.contentFrame)
protected FrameLayout mContentFrame;
private ActionBarDrawerToggle drawerToggle;
protected abstract @LayoutRes int getContentLayout();
private Intents mIntents;
private String[] mOptionsTitles = new String[]{
"Menu item 1",
"Menu item 2",
"Menu item 3"
};
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.drawer_activity);
getLayoutInflater().inflate(getContentLayout(), mContentFrame);
ButterKnife.bind(this);
}
活动类
@Override
protected int getContentLayout() {
return R.layout.activity_main;
}
目前,我留下了一个选项删除Butterknife,或重复所有活动中抽屉处理的逻辑。 两种选择都倾向于避免。