Question

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;
    }
Was it helpful?

Solution

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};

OTHER TIPS

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

Try this..

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

Instead of this..

one = findViewById(R.id.switchOne);

EDIT

switchArray[0] = (Switch) findViewById(R.id.switchOne);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top