本文介绍了Android的&放大器; RoboGuice - 注入的片段看法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有,我需要在屏幕上显示一个片段。我希望能够使用 InjectView 来注入我的UI元素。 InjectView工作正常的活动,因为视图(XML)是在的onCreate 然而在碎片视图设置在 onCreatView 。

那么,有没有办法使用InjectView的片段?我知道,我可以用findViewbyId找到每一个元素,但我宁愿使用InjectView

 公共类ProfileFragment扩展RoboDialogFragment {

    @InjectView(R.id.commentEditText)
    受保护的EditText commentEditText;

    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);

            //我在这里得到一个空指针异常
            commentEditText.setText(有些评论);

    }

    @覆盖
    公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,捆绑savedInstanceState){
        查看查看= inflater.inflate(R.layout.profile,集装箱,假);

            //我在这里得到一个空指针异常
        commentEditText.setText(有些评论);

        返回查看;
    }

}
 

解决方案

在注射过程中 onViewCreated

情况

  @覆盖
公共无效onViewCreated(查看视图,捆绑savedInstanceState){
    super.onViewCreated(查看,savedInstanceState);

    commentEditText.setText(有些评论);
}
 

I have a fragment that I need to display on the screen. I want to be able to use InjectView to inject my UI elements. InjectView works fine on activities because the view (xml) is set during onCreate, however on fragments the view is set on onCreatView.

So is there a way to use InjectView on fragments? I know that I could use findViewbyId to find each element, but I rather use 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;
    }

}
解决方案

Injection happens during onViewCreated

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

    commentEditText.setText("Some comment");
}

这篇关于Android的&放大器; RoboGuice - 注入的片段看法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 22:55