Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android & RoboGuice - Inject views on Fragment?

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;
    }

}
like image 860
aryaxt Avatar asked Mar 20 '12 02:03

aryaxt


People also ask

Is Android good or Apple?

Apple and Google both have fantastic app stores. But Android is far superior at organizing apps, letting you put important stuff on the home screens and hide less useful apps in the app drawer. Also, Android's widgets are much more useful than Apple's.

What's meant by Android?

Android OS is a Linux-based mobile operating system that primarily runs on smartphones and tablets. The Android platform includes an operating system based upon the Linux kernel, a GUI, a web browser and end-user applications that can be downloaded.

What is the latest Android operating system?

The latest version of Android OS is 12, released in October 2021. Learn more about OS 12, including its key features. Older versions of Android include: Tiramisu (OS 13)

Who is the owner of Android?

Android Inc., was bought by the American search engine company Google Inc., in 2005. At Google, the Android team decided to base their project on Linux, an open source operating system for personal computers.


1 Answers

Injection happens during onViewCreated

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

    commentEditText.setText("Some comment");
}
like image 95
aryaxt Avatar answered Nov 15 '22 16:11

aryaxt