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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:11:33+00:00 2026-05-25T22:11:33+00:00

I’m in the process of making an Android 3 app that has a tab-based

  • 0

I’m in the process of making an Android 3 app that has a tab-based navigation. Being quite new to Android I searched for tab tutorials and found a few different approaches, out of which an implementation based on Fragments + ActionBar seemed to be the best. I followed these tutorials

http://www.abelski.com/courses/android3ui/actionbar.pdf (the last few slides) and
http://www.youtube.com/watch?v=gMu8XhxUBl8 (same lesson in video format)

…and got to the point where the tabs show up, I can switch between them OK, and simple TextViews inside them show up fine.

Now, problems arise when I want one of the tabs to have a spinner widget that is defined in the fragment’s XML file and populate it with items that are defined in the strings.xml file. When I try to access the spinner with getViewById() from the onCreate() method I always get a null value as a result.

As I understand it, to fix this I need to somehow declare the XML of the fragment (similarly like main layout has been done with setContentView(R.layout.main)) so that getViewById() knows where to look for the spinner (Eclipse’s autocomplete finds it already though). However, I’m at complete loss how to do this, since none of the tutorials tackled the scenario where you want to modify the XML-based Fragments’ contents inside the code.

The main.xml looks like this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">


<LinearLayout android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:id="@+id/fragment_placeholder"></LinearLayout>

</LinearLayout>

tab1_fragment.xml like this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical" 
              android:layout_width="fill_parent" 
              android:layout_height="fill_parent">

    <Spinner android:id="@+id/spinner1" 
             android:layout_width="wrap_content" 
             android:layout_height="wrap_content"/>
</LinearLayout>

and onCreate method like this:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    ActionBar ab = getActionBar();
    ab.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    ActionBar.Tab newAuditTab = ab.newTab().setText(R.string.tabheader_new_audit);
    Fragment newAuditFrag = new FragmentTabNewAudit();
    newAuditTab.setTabListener(new TabListener(newAuditFrag));
    ab.addTab(newAuditTab);

    //and the same for other tabs

    Spinner spinner = (Spinner) findViewById(R.id.spinner1); // this returns null
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
            this, R.array.cell_array, android.R.layout.simple_spinner_item); //cell_array is defined in strings.xml


    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter);
    spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());  
}

Accessing the views that are defined in main.xml via R.id works fine, so in this case the problem isn’t related to Eclipse’s auto-importing of the R class, I think.

I guess I could define the spinner programmatically to get around the issue, but there is probably a more elegant solution to this, I hope. I can’t see the Android big picture well enough yet to quite know where to look.

  • 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-05-25T22:11:33+00:00Added an answer on May 25, 2026 at 10:11 pm

    As the spinner is not a child of the Activity itself, findViewById can not find it. As the spinner is a child of the fragment, you might try:

    Spinner spinner = (Spinner)newAuditFrag.getView().findViewById(R.id.spinner1);
    

    However, I’m not sure when the view for a fragment is created, so getView() might also return null that that point in your code. You could then try to move the code into onResume, which is called after onCreate.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
Basically, what I'm trying to create is a page of div tags, each has
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
We're building an app, our first using Rails 3, and we're having to build
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is

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.