Acceda a la vista de fragmentos desde la actividad onCreate

I am in the process of making my first app for Android, and I have a Fragment que se agrega a mi Activity en el capítulo respecto a la Activity's onCreate() method. The problem I am facing is that I am unable to find any of the views contained within the Fragment de Activity's onCreate() método.

Other threads have suggested that this is because the Fragment has not yet been inflated, so findViewById() will return null for any views contained within the Fragment.

Esto es lo que quiero decir:

Actividad:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    System.out.println("activity onCreate");
    setContentView(R.layout.activity_main);

    if (savedInstanceState != null) {
        return;
    }

    initialiseUI(); // Fragment added to Activity

    System.out.println("end of activity onCreate");
}

Fragmento:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
    Bundle savedInstanceState) {
System.out.println("fragment onCreateView");
    return inflater.inflate(R.layout.event_log, container, false);
}

This prints the results:

activity onCreate end of activity onCreate fragment onCreateView

Because of this order, any attempt to access the views of the Fragment en el capítulo respecto a la Activity's onCreate() método (usando findViewById()) produce un NullPointerException, Como el Fragment's onCreateView() only gets called AFTER the end of the Activity's onCreate().

Usando el FragmentManger's executePendingTransactions() después de agregar el Fragment no ayuda.

Basically, I have been forced to put the problem code in the Activity's onStart() método en lugar de onCreate(), ya que onStart() happens AFTER the Fragment's onCreateView().

Does anyone what the standard practice here is, or how I can make my Fragment-View-accessing code work within the Activity's onCreate() ¿método?

preguntado el 25 de agosto de 12 a las 14:08

Why do you want to access Fragment's views in Activity's onCreate() method? Something is wrong with your desing. -

Specifically, after adding a Fragment in the Activity's onCreate() method, I want to change the text of a TextView contained within the Fragment, and add some items to a Spinner. What could be wrong with my design? -

Than update your fragment's layout in Fragment's onCreateView() método. Mira mi respuesta. -

1 Respuestas

Update your views in onCreateView().

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
    Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.event_log, container, false);
    TextView tv = (TextView) view.findViewById(R.id.text);
    tv.setText("hello world");
    return view;
}

Or if your changes depend on Activity su Fragment is attached to, use onActivityCreated().

@Override
public void onActivityCreated (Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    TextView tv = (TextView) getView().findViewById(R.id.text);
    tv.setText(getActivity.getSomeText());
}

Respondido el 02 de Septiembre de 12 a las 11:09

Ok, after a million other problems that I won't go into, I finally got this working properly! Thank you for your help. - Dan

@Dan, can you share exactly the other problems and how you used the above to solve it? It would be a great contribution ot the community. - mike macintosh

Basically, I tried using onActivityCreated() to initialise my components, thinking this method would only be called once. This caused a lot of problems when pausing and restarting my app, as this method was resetting all of my components, since it gets called each time. Then, when trying to make this work using onCreateView(), I used getView().findViewById(...) rather than using the already-initialised "view" variable. It was a silly mistake, but I got there in the end! So now the text is stored to a field in onCreate(), and put into the TextView in onCreateView(). - Dan

Por favor vea mi pregunta relacionada: stackoverflow.com/questions/24833912/… - dr jacky

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.