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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T02:05:34+00:00 2026-05-17T02:05:34+00:00

For an Android application, I’m trying to use a Spinner which, when an option

  • 0

For an Android application, I’m trying to use a Spinner which, when an option is selected, will hide/display relevant View objects. For my application, these objects are an EditText and an associated TextView label for the field. Unfortunately, I can’t seem to get the EditText to hide/display, and when I add it code to hide/display the TextView, I get a NullPointerException. I presume that since I am laying out the view objects in a RelativeLayout, by hiding one of the view objects, I am removing its relationship with other view objects, hence the NullPointer.

Can anyone figure out why this might be happening? Here’s my code:

public class FormFields extends Activity {
    private Spinner mSpinner;
    private EditText mTextField;
    private TextView mLabel;

    private static final int SPINNER_OPTION_FIRST = 0;
    private static final int SPINNER_OPTION_SECOND = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.form_fields);

        mTextField = (EditText) findViewById(R.id.text_field);
        mLabel = (TextView) findViewById(R.id.field_label)
        mSpinner = (Spinner) findViewById(R.id.spinner);

        ArrayAdapter adapter1 = ArrayAdapter.createFromResource(
            this, R.array.spinnerOptions, android.R.layout.simple_spinner_item);
        adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        mSpinner.setAdapter(adapter1);

        mSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
                switch(position) {
                    case SPINNER_OPTION_FIRST: {
                        mLabel.setVisibility(View.GONE);
                        mTextField.setVisibility(View.GONE);
                    }
                    case SPINNER_OPTION_SECOND: {
                        mLabel.setVisibility(View.VISIBLE);
                        mTextField.setVisibility(View.VISIBLE);
                    }
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parentView) {
                // Do nothing
            }
        });
    }
}

form_fields.xml

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

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#104667">

        <TextView
            android:id="@+id/spinner_label"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="15dip"
            android:textStyle="bold"
            android:text="Please select an option" />

        <Spinner
            android:id="@+id/spinner"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/spinner_label"
            android:layout_marginLeft="25dip"
            android:layout_marginRight="25dip"
            android:drawSelectorOnTop="true"
            android:prompt="@string/spinnerPrompt" />

        <TextView
            android:id="@+id/field_label"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/spinner"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="15dip"
            android:textStyle="bold"
            android:text="Enter text here: "
            android:visibility="gone" />

        <EditText
            android:id="@+id/text_field"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="25dip"
            android:layout_marginRight="25dip"
            android:layout_below="@+id/field_label" 
            android:visibility="gone" />
    </RelativeLayout>
</ScrollView>
  • 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-17T02:05:34+00:00Added an answer on May 17, 2026 at 2:05 am

    There are a few small omissions in the code you posted. When I made the following changes, I was able to compile and run your code successfully.

    1. You are missing a semicolon after

      mLabel = (TextView) findViewById(R.id.field_label)
      
    2. Insert a break; statement in between your two case options.

    3. You can remove unnecessary braces around your case statements.

      case SPINNER_OPTION_FIRST:
          mLabel.setVisibility(View.GONE);
          mTextField.setVisibility(View.GONE);
          break;        
      case SPINNER_OPTION_SECOND:
          mLabel.setVisibility(View.VISIBLE);
          mTextField.setVisibility(View.VISIBLE);
      
    4. Although not necessary to get your program to run, it would be better to explicitly specify ArrayAdapter<CharSequence> when defining adapter1 to avoid type issues.

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

Sidebar

Related Questions

Our android application is getting fairly big and we would like to use functional
In my Android application, I have a simple list view with adapter. There's a
My Android application contains an EditText view where you can type some short messages
My android application need to print over wifi printer.Here wifi printer will be connected
my Android application needs to play .swf files, which are sound only.. Is there
in my android application it does not seem to let me use the accelerometer.
I am writing an android application which has a lot of images in it.
My Android application needs to show a dialog on first use of the application.
In android application for data storing and sharing in app which one is good
My Android application is a foreground service and I would like the option of

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.