Android ChildActivity扩展了BaseActivity,如何将布局集中的视图绑定到LayoutInflater::inflate?

Android ChildActivity扩展了BaseActivity,如何将布局集中的视图绑定到LayoutInflater::inflate?,android,android-layout,butterknife,Android,Android Layout,Butterknife,我在尝试用ButterKnife成功地注入视图时遇到了困难。我看到的所有示例都假定活动扩展了AppCompatActivity,布局是用setContentView()设置的。我的案例涉及活动扩展基本活动,并使用LayoutInflater的充气()调用设置布局: public class BaseActivity extends AppCompatActivity { @BindView(R.id.drawer_layout) DrawerLayout drawerLayout;

我在尝试用ButterKnife成功地注入视图时遇到了困难。我看到的所有示例都假定
活动
扩展了
AppCompatActivity
,布局是用
setContentView()
设置的。我的案例涉及
活动
扩展
基本活动
,并使用
LayoutInflater
充气()调用设置布局:

public class BaseActivity extends AppCompatActivity {
    @BindView(R.id.drawer_layout) DrawerLayout drawerLayout;
    @BindView(R.id.toolbar) Toolbar toolbar;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.base);
        ButterKnife.bind(this);
    }
}
这是
ChildActivity

public class ChildActivity extends BaseActivity {
    @BindView(R.id.content) FrameLayout content; // content is in the base layout
    @BindView(R.id.recycler_view) RecyclerView recyclerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // content (below) is a FrameLayout in the BaseActivity
        getLayoutInflater().inflate(R.layout.child, content);
        ButterKnife.bind(this);
    }
}
当我运行应用程序时,我得到一个错误:

Required view 'recycler_view' with ID 2131230798 for field 'recyclerView' 
was not found. If this view is optional add '@Nullable' (fields) or 
'@Optional' (methods) annotation.
因此,我按照建议添加了
@Nullable

 @BindView(R.id.recycler_view) @Nullable RecyclerView recyclerView;
另一个错误:

java.lang.NullPointerException: Attempt to invoke virtual method 'void
 android.support.v7.widget.RecyclerView.setLayoutManager(android.support.v
7.widget.RecyclerView$LayoutManager)' on a null object reference
当我删除
@Nullable
时,我又回到了原点。我如何解决这个问题

使用LayoutFlater的充气()调用设置布局

getLayoutInflater().inflate
返回一个全新的视图,该视图需要与活动内容视图分开绑定。如果您想使用该视图,则不需要对其进行充气,因为您只需使用布局id调用setContentView

不过,如果您想在框架布局中实现这一点,我建议您使用片段

您将
@BindView
框架布局,然后使用
getSupportFragmentManager()
. 是否在片段中使用Butterknife是一个实现细节

至少,在XML中使用
标记,而不是框架布局。存在错误,因为Recyclerview不在绑定实例的上下文中

使用LayoutFlater的充气()调用设置布局

getLayoutInflater().inflate
返回一个全新的视图,该视图需要与活动内容视图分开绑定。如果您想使用该视图,则不需要对其进行充气,因为您只需使用布局id调用setContentView

不过,如果您想在框架布局中实现这一点,我建议您使用片段

您将
@BindView
框架布局,然后使用
getSupportFragmentManager()
. 是否在片段中使用Butterknife是一个实现细节


至少,在XML中使用
标记,而不是框架布局。存在错误是因为Recyclerview不在绑定实例的上下文中

此答案太短<代码>视图v=GetLayoutFlater().充气(R.layout.child,content)
后面跟着
Butterknife.bind(this,v)
也是我尝试过的。同样的错误!因为
这个
是活动,它只有一个框架布局、抽屉布局和工具栏。请参阅我对ChildActivity类所做的编辑。我忘了添加
@BindView(R.id.content)FrameLayout内容这是BaseActivity中的框架布局。这会改变什么吗?否则你是说我现在试图做的事情在活动中是不可能的?否则我就不可能再回到碎片了,不是真的。您的RecyclerView不在活动内容视图中此答案太短<代码>视图v=GetLayoutFlater().充气(R.layout.child,content)
后面跟着
Butterknife.bind(this,v)
也是我尝试过的。同样的错误!因为
这个
是活动,它只有一个框架布局、抽屉布局和工具栏。请参阅我对ChildActivity类所做的编辑。我忘了添加
@BindView(R.id.content)FrameLayout内容这是BaseActivity中的框架布局。这会改变什么吗?否则你是说我现在试图做的事情在活动中是不可能的?否则我就不可能再回到碎片了,不是真的。您的RecyclerView不在活动内容视图中请参阅此链接:请参阅此链接: