Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8438077
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T07:36:31+00:00 2026-06-10T07:36:31+00:00

I am in the process of making my first app for Android, and I

  • 0

I am in the process of making my first app for Android, and I have a Fragment that gets added to my Activity in the Activity‘s onCreate() method. The problem I am facing is that I am unable to find any of the views contained within the Fragment from the Activity‘s onCreate() method.

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.

Here is what I mean:

Activity:

@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");
}

Fragment:

@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 in the Activity‘s onCreate() method (using findViewById()) produces a NullPointerException, as the Fragment‘s onCreateView() only gets called AFTER the end of the Activity‘s onCreate().

Using the FragmentManger‘s executePendingTransactions() after adding the Fragment doesn’t help.

Basically, I have been forced to put the problem code in the Activity‘s onStart() method instead of onCreate(), as 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() method?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T07:36:32+00:00Added an answer on June 10, 2026 at 7:36 am

    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 your 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());
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello I am in the process of making an Android app that pulls some
I am currently in the process of making my first iphone app with a
I'm currently in the process of making one of my first android games and
I am currently in the process of making my first Titanium iPhone app. In
I am making my first app which will use push notifications. I have decided
I am in the process of making a website that involves a shopping cart.
I am in the process of building my first iPhone app (xCode,objective-c) which has
I'm using Xode 4, and i have recently uploaded my first app... but now
So I'm making an iOS app that constantly sends and array of points through
I have a web app that currently uses the current HttpContext to store a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.