我有一个片段,我需要在屏幕上显示.我希望能够InjectView用来注入我的UI元素.InjectView在活动中工作正常,因为视图(xml)是在设置期间设置的onCreate,但是在片段上设置了视图onCreatView.
那么有没有办法在片段上使用InjectView?我知道我可以使用findViewbyId来查找每个元素,但我更喜欢使用InjectView
public class ProfileFragment extends RoboDialogFragment {
@InjectView(R.id.commentEditText)
protected EditText commentEditText;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// I get a null pointer exception here
commentEditText.setText("Some comment");
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.profile , container, false);
// I get a null pointer exception here
commentEditText.setText("Some comment");
return view;
}
}
Run Code Online (Sandbox Code Playgroud) 我在找到使用片段+ RoboGuice的工作示例时遇到了问题.当您尝试使用Android片段事务管理器添加/删除片段时,会发生此问题.一旦告诉片段继承自RoboFragment,事务管理器就不再认为该类是片段(因为它扩展了RoboFragment).但是,您可以使用RoboGuice自己的片段管理器,但它也会崩溃.有没有动态添加/删除RoboGuice片段的例子?