Android和RoboGuice - 在Fragment上注入视图?

ary*_*axt 15 android inject fragment roboguice

我有一个片段,我需要在屏幕上显示.我希望能够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)

ary*_*axt 27

注射发生在 onViewCreated

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    commentEditText.setText("Some comment");
}
Run Code Online (Sandbox Code Playgroud)

  • 我们将努力为2.0获得更好的文档,因为它在发布候选版本中 (2认同)