fragment - getView() method return NULL

781 views Asked by At

I don't know why in my fragment when i call getView it return NULL.

I Know that i can call getView just after the execution of the onCreateView method
I debugged my app: and i checked that first is called the onCreateView method.
So i don't know where is the problem.

I show to you my code:
this is my main activity and when a user click in the drawer menu i check which voice of the menu is pressed:

public void onNavigationDrawerItemSelected(int position) {
     ...    
     if (position == 7) {
                CreaVotoFragment fragment = CreaVotoFragment.newInstance(position + 1);
                FragmentManager fragmentManager = getFragmentManager();
                fragmentManager.beginTransaction()
                        .replace(R.id.container, fragment)
                        .commit();
                //Mostra un Dialog che consente l'eliminazione di un corso
                fragment.createMark(this);
     }
     ...
 }

In this case the user pressed the voice menu number 7, so inside this block I create a new Fragment, with the static method newIstance.

this is the method:

public static CreaVotoFragment newInstance(int sectionNumber) {
    CreaVotoFragment fragment = new CreaVotoFragment();
    Bundle args = new Bundle();
    args.putInt(ARG_SECTION_NUMBER, sectionNumber);
    fragment.setArguments(args);
    return fragment;
}

after that i simply give the fragment to the method: replace.

it work and i can also see that he call onCreateView method overrided in the class CreaVotoFragment.

The problem begin when i call with the createMark method (last line of the if block).
inside of the createMark method, when i call getView, return to me a null pointer.

1

There are 1 answers

2
slesar On BEST ANSWER

You should notice that commit() will run on Handler.post(). That means that your fragment's view will be created AFTER onNavigationDrawerItemSelected.

Better if you call view-related-methods from inside fragment to constrain view's lifecycle.