Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fragment UI elements are null

I add fragments to my activity programatically in a for loop like this:

    FragmentManager fm = getFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();
    for(int i = 0; i<bl.size(); i++)
    {ft.add(R.id.linearlayout, new BackflowDeviceFragment(), Integer.toString(i));}//for
    ft.commit();
    fm.executePendingTransactions();

I then attempt to obtain references to these fragments like so:

BackflowDeviceFragment frag = (BackflowDeviceFragment) fm.findFragmentByTag(Integer.toString(i));

I successfully get a reference, however whenever I try to operate on any of the fragments' UI elements they are found to be null. So I did some debugging and found that the public methods that I created to operate on the UI elements are being called before the onCreateView() method where I set up all the UI stuff.

When is onCreateView() called? How can I successfully work with these UI elements?

like image 223
DrS Avatar asked Sep 25 '26 06:09

DrS


1 Answers

According to Fragment Documentation, onCreateView() :

This will be called between onCreate(Bundle) and onActivityCreated(Bundle). 

so you shouldn't reference your UI Elements before Initializing.

like image 192
Arash GM Avatar answered Sep 27 '26 20:09

Arash GM