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 7722463
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T04:10:53+00:00 2026-06-01T04:10:53+00:00

I’m trying to get a Spinner View from an added Fragment to my activity

  • 0

I’m trying to get a Spinner View from an added Fragment to my activity but it seems that I cannot get the fragment nor the view.

I have an Activity which adds a Fragment to it’s LinearLayout, the fragment’s layout is based on the ‘extra’ that comes from the intent of the Activity. Everything is displayed correctly and I can see all the views of the fragment but for some reason when I call findFragmentByTag(String tag) it returns null and thus I cannot call getView().

Here is how I am adding the fragment code to my activity:

...onCreate(){
    ...
    FragmentManager fm = getFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();

    Fragment fr = new Fragment(){
        @Override
        public View onCreateView(LayoutInflater inflater,
                ViewGroup container, Bundle savedInstanceState) {
            return inflater.inflate(layoutID, container, false);
        }
    };
    ft.add(R.id.formsListActivity_Container, fr, "form_fragment_tag");

    ft.commit();
    ...

}

Then I try to get such fragment and get a the spinner view to add the string array:

Fragment f = fm.findFragmentByTag("form_fragment_tag");
Spinner spinner = (Spinner) f.getView().findViewById(R.id.city_spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, 
        R.array.city_list, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);

But I get a null exception because findFragmentByTag() returns null

What am I doing wrong? I’ve verified the code a few times and the tags are the same.
I’ve also make sure the spinner’s id is in the XML layout file correctly, and the fragment is loading the correct XML layout because I can see it (If a comment the get view from fragment part).

  • 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-01T04:10:54+00:00Added an answer on June 1, 2026 at 4:10 am

    I have resolved the problem, it seems that based on the life cycle of both the activity and the fragment by the time the findFragmentById() is executed in the onCreate() the fragment has not been ‘actually created’, I tested this by adding a simple button to the layout and making a method that when clicked on the button the spinner will be filled with data.

    But of course I don’t want the user to click a ‘Load’ button to fill the spinner, instead I have implemented on my fragment the public void onActivityCreated() method.

    For reference here is my code of the fragment:

     Fragment fr = new Fragment(){
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            return inflater.inflate(layoutID, container, false);
        }
    
        @Override
        public void onActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);
            Spinner spinner = (Spinner) this.getView().findViewById(R.id.city_spinner);
            ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(context, R.array.city_list, android.R.layout.simple_spinner_item);
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            spinner.setAdapter(adapter);
        }
    
     };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have a view passing on information from a database: def serve_article(request, id): served_article
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms
I have a text area in my form which accepts all possible characters from
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;
I'm trying to create an if statement in PHP that prevents a single post

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.