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

  • Home
  • SEARCH
  • 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 9155749
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:39:05+00:00 2026-06-17T12:39:05+00:00

I have a basic Android Application with 2 Fragments. I am trying to fill

  • 0

I have a basic Android Application with 2 Fragments.

I am trying to fill a Spinner in one of the Fragments with some String. Here is my method (BTW, this all works if I comment out the whole Try statement in the onCreate.

public class RadarSelectionFragment extends Fragment {
    /**
     * The fragment argument representing the section number for this
     * fragment.
     */
    public static final String ARG_SECTION_NUMBER = "section_number";

    public RadarSelectionFragment() {
    }

    @Override
    public final View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Create a new TextView and set its text to the fragment's section
        // number argument value.\
        if (getArguments().getInt(ARG_SECTION_NUMBER) == 1) {
            return inflater.inflate(R.layout.radars, container, false);
        } else {
            return container;
        }

    }

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        try {
            Spinner s = (Spinner) findViewById(R.id.stateselector);
            ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getActivity(), R.array.states, android.R.layout.simple_spinner_item);
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            s.setAdapter(adapter);
        } catch (Exception e) {
            Toast.makeText(getActivity(), e.toString(), 1).show();
        }
    }
} 

The R.array.states is an array containing the states and territories of Australia and the R.id.stateselector is defined as:

    <Spinner
        android:id="@+id/stateselector"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"/>

And displays with no items if I comment out the Try statement.

This is all done following the tutorial from – http://developer.android.com/guide/topics/ui/controls/spinner.html

Any help is greatly appreciated!!!

EDIT 1: Stacktrace

01-20 22:25:09.402: E/AndroidRuntime(8442): FATAL EXCEPTION: main
01-20 22:25:09.402: E/AndroidRuntime(8442): java.lang.NullPointerException
01-20 22:25:09.402: E/AndroidRuntime(8442):     at com.example.radarau.MainActivity$RadarSelectionFragment.onCreate(MainActivity.java:176)
01-20 22:25:09.402: E/AndroidRuntime(8442):     at android.support.v4.app.Fragment.performCreate(Fragment.java:1437)
01-20 22:25:09.402: E/AndroidRuntime(8442):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:877)
01-20 22:25:09.402: E/AndroidRuntime(8442):     at android.support.v4.app.FragmentManagerImpl.performPendingDeferredStart(FragmentManager.java:807)
01-20 22:25:09.402: E/AndroidRuntime(8442):     at android.support.v4.app.Fragment.setUserVisibleHint(Fragment.java:801)
01-20 22:25:09.402: E/AndroidRuntime(8442):     at android.support.v4.app.FragmentPagerAdapter.setPrimaryItem(FragmentPagerAdapter.java:130)
01-20 22:25:09.402: E/AndroidRuntime(8442):     at android.support.v4.view.ViewPager.populate(ViewPager.java:1010)
01-20 22:25:09.402: E/AndroidRuntime(8442):     at android.support.v4.view.ViewPager.populate(ViewPager.java:881)
01-20 22:25:09.402: E/AndroidRuntime(8442):     at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1366)
01-20 22:25:09.402: E/AndroidRuntime(8442):     at android.view.View.measure(View.java:15513)
01-20 22:25:09.402: E/AndroidRuntime(8442):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4827)
01-20 22:25:09.402: E/AndroidRuntime(8442):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
01-20 22:25:09.402: E/AndroidRuntime(8442):     at android.view.View.measure(View.java:15513)
01-20 22:25:09.402: E/AndroidRuntime(8442):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
01-20 22:25:09.402: E/AndroidRuntime(8442):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
01-20 22:25:09.402: E/AndroidRuntime(8442):     at android.view.View.measure(View.java:15513)
01-20 22:25:09.402: E/AndroidRuntime(8442):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4827)
01-20 22:25:09.402: E/AndroidRuntime(8442):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
01-20 22:25:09.402: E/AndroidRuntime(8442):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2176)
01-20 22:25:09.402: E/AndroidRuntime(8442):     at android.view.View.measure(View.java:15513)
01-20 22:25:09.402: E/AndroidRuntime(8442):     at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1874)
01-20 22:25:09.402: E/AndroidRuntime(8442):     at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1089)
01-20 22:25:09.402: E/AndroidRuntime(8442):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1265)
01-20 22:25:09.402: E/AndroidRuntime(8442):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
01-20 22:25:09.402: E/AndroidRuntime(8442):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
01-20 22:25:09.402: E/AndroidRuntime(8442):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
01-20 22:25:09.402: E/AndroidRuntime(8442):     at android.view.Choreographer.doCallbacks(Choreographer.java:562)
01-20 22:25:09.402: E/AndroidRuntime(8442):     at android.view.Choreographer.doFrame(Choreographer.java:532)
01-20 22:25:09.402: E/AndroidRuntime(8442):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
01-20 22:25:09.402: E/AndroidRuntime(8442):     at android.os.Handler.handleCallback(Handler.java:725)
01-20 22:25:09.402: E/AndroidRuntime(8442):     at android.os.Handler.dispatchMessage(Handler.java:92)
01-20 22:25:09.402: E/AndroidRuntime(8442):     at android.os.Looper.loop(Looper.java:137)
01-20 22:25:09.402: E/AndroidRuntime(8442):     at android.app.ActivityThread.main(ActivityThread.java:5039)
01-20 22:25:09.402: E/AndroidRuntime(8442):     at java.lang.reflect.Method.invokeNative(Native Method)
01-20 22:25:09.402: E/AndroidRuntime(8442):     at java.lang.reflect.Method.invoke(Method.java:511)
01-20 22:25:09.402: E/AndroidRuntime(8442):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-20 22:25:09.402: E/AndroidRuntime(8442):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-20 22:25:09.402: E/AndroidRuntime(8442):     at dalvik.system.NativeStart.main(Native 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-17T12:39:05+00:00Added an answer on June 17, 2026 at 12:39 pm

    You can’t use findViewById in onCreate method because the view hierarchy is not created yet. Try to move your code to onActivityCreated:

    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    
        try {
            Spinner s = (Spinner) findViewById(R.id.stateselector);
            ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getActivity(), R.array.states, android.R.layout.simple_spinner_item);
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            s.setAdapter(adapter);
        } catch (Exception e) {
            Toast.makeText(getActivity(), e.toString(), 1).show();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi. I have create a background service application following this tutorial http://androidsourcecode.blogspot.com/2010/10/basic-android-background-service.html . But
I have written one simple Android Tetris application. After it I decided to read
We have a very basic application (iOS/Android) done in Appcelerator that will receive a
I'm just starting to dive into some basic Android development and have been experimenting
I have been trying to build Pocketsphinx for my android application and I have
I am developing an Android application and I have some data saved locally in
I have a very basic application in which I am trying to create ListFragment
I am performing HTTP Basic Authentication in my Android application. Recently, however, I have
I am developing an android application with one basic function, to allow the user
I have ported Apache Tika to Android. I have a basic question. While working

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.