我试图将浮动按钮添加到片段中,但是使用findViewById创建按钮时出现错误。
public class HomeFragment extends Fragment {
FloatingActionButton fab;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_home, container,false);
fab = (FloatingActionButton) getView().findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(getActivity(), ProfileActivity.class));
}
});
}
}
错误:fab =(FloatingActionButton)getView()。findViewById(R.id.fab);
“无法到达的陈述”
神不在的星期二
相关分类