0

I have a collection of switches in android; let's call them...

Switch one;
Switch two;
Switch three;
Switch four;
Switch five;

and I also have an array which contains these views.

Switch[] switchArray = {one, two, three, four, five};

and then, within my onCreateView() method; I assign all of those Switches using findViewById().

one = (Switch) findViewById(R.id.switchOne);
two = (Switch) findViewById(R.id.switchTwo);
three = (Switch) findViewById(R.id.switchThree);
four = (Switch) findViewById(R.id.switchFour);
five = (Switch) findViewById(R.id.switchFive);

Now; when I create a for-loop to check if that array is null:

    for(int i = 0; i<switchArray.length; i++){
      if(switchArray[i] == null){
         Log.d(TAG, "Array is null at:" + i);
     }
    }

I get the following logcat:

Array is null at: 1
Array is null at: 2
Array is null at: 3
Array is null at: 4
Array is null at: 5

and I am unsure as to why these variables are returning null if I tried to instantiate them in onCreate... if I also try to instantiate them before the onCreate (in the class header before any methods); I still get the same error.

Hopefully the problem is clear.

EDIT 1: FULL ONCREATEVIEW METHOD

public View onCreateView(LayoutInflater viewInflation, ViewGroup container,
            Bundle SavedInstantState) {
        superContext = getActivity().getApplicationContext();
        digitalfragmentview = viewInflation.inflate(
                R.layout.digitalfragment_page, container, false);

        digitalIO0Mode = (Switch) digitalfragmentview
                .findViewById(R.id.digitalio0mode);
        digitalIO1Mode = (Switch) digitalfragmentview
                .findViewById(R.id.digitalio1mode);
        digitalIO2Mode = (Switch) digitalfragmentview
                .findViewById(R.id.digitalio2mode);
        digitalIO3Mode = (Switch) digitalfragmentview
                .findViewById(R.id.digitalio3mode);
        digitalIO4Mode = (Switch) digitalfragmentview
                .findViewById(R.id.digitalio4mode);
        digitalIO5Mode = (Switch) digitalfragmentview
                .findViewById(R.id.digitalio5mode);
        digitalIO6Mode = (Switch) digitalfragmentview
                .findViewById(R.id.digitalio6mode);
        digitalIO7Mode = (Switch) digitalfragmentview
                .findViewById(R.id.digitalio7mode);
        digitalIO8Mode = (Switch) digitalfragmentview
                .findViewById(R.id.digitalio8mode);
        digitalIO9Mode = (Switch) digitalfragmentview
                .findViewById(R.id.digitalio9mode);

        // sets the listener for the mode switches
        for (int i = 0; i < digitalIOModeSwitchArray.length; i++) {
            if (digitalIOModeSwitchArray[i] == null) {
                Log.d(TAG, "Array is null at:" + i);
            }
        }

        return digitalfragmentview;
    }
5
  • When are you instantiating switchArray ? You need to instantiate it after you do your "findViewById" calls Commented Mar 3, 2014 at 15:55
  • the switchArray is instantiated before the onCreateView(); in the top of the class with all other class variables. Commented Mar 3, 2014 at 15:56
  • in my latest edition; digitalIO0Mode is equivalent to Switch One; etc etc... Commented Mar 3, 2014 at 15:59
  • You array will not be updated this way. You should instantiate your array after your findViewById calls. Commented Mar 3, 2014 at 16:01
  • Okay I will attempt that now and get back to you. Commented Mar 3, 2014 at 16:02

3 Answers 3

3
Switch[] switchArray = {one, two, three, four, five};

contains references to one, two, three, four, five at the moment you create the array.

Latter, you reassign those variables, but your array still points to the previous references.

You need to create the array after you have assigned your views:

one = (Switch) findViewById(R.id.switchOne);
two = (Switch) findViewById(R.id.switchTwo);
three = (Switch) findViewById(R.id.switchThree);
four = (Switch) findViewById(R.id.switchFour);
five = (Switch) findViewById(R.id.switchFive);
// Then only
switchArray = {one, two, three, four, five};
Sign up to request clarification or add additional context in comments.

3 Comments

Is there a way I can access that array with methods within my class then if it is now out of the scope of other methods within my class?
did you receive the comment notification?
you don't need to change it's scope. you can declare it as a member of your class, and still define it in the onCreate method.
2

First take references to your widget in variables one = (Switch) findViewById(R.id.switchOne); two = (Switch) findViewById(R.id.switchTwo); three = (Switch) findViewById(R.id.switchThree); four = (Switch) findViewById(R.id.switchFour); five = (Switch) findViewById(R.id.switchFive);

then put them in an array

Switch[] switchArray = {one, two, three, four, five};

The reason is that Switch array is value type not reference type

Comments

1

Try this..

one = (Switch) findViewById(R.id.switchOne);

Instead of this..

one = findViewById(R.id.switchOne);

EDIT

switchArray[0] = (Switch) findViewById(R.id.switchOne);

5 Comments

My apologies; I had that already I incorrectly formatted my code. Please refer to my latest update.
@Tukajo can you post your full onCreateView()
the additions have been made.
@Tukajo Is that all switches are in digitalfragment_page.xml
Yes; they are all in digitalfragment_page.xml.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.