jeudi 12 juillet 2018

Always null returned (NPE) on getChildCount - checked everything

OK, so I have multiple RelativeLayouts in different XML files in the layout directory. Each is ID'd properly, declared properly, and initialized properly in Java MainActivity.class. These XML files are used by the include tag. I want to un-check all of the CheckBoxes in each RelativeLayout in each XML file. I use the following code (this is for one of the RL's):

RelativeLayout rWen; //above onCreate

rWen = (RelativeLayout)findViewById(R.id.relWends); //in onCreate

    for (int w = 0; w < rWen.getChildCount(); w++) {  //in public void called from button)
        View v = rWen.getChildAt(w);
        if (v instanceof CheckBox) {
            CheckBox wB = (CheckBox)v;
            wB.setChecked(false);
        }
    }

My problem is no matter what I check, Android Studio tells me that rWen has NO children. It's a NPE that says getChildCount returns NULL.

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

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/relWends"
    android:gravity="center_horizontal">

    <CheckBox
        ...params/>

    <CheckBox
        ...params/>

    <CheckBox
        ...params/>

    <CheckBox
        ...params/>

</RelativeLayout>
</ScrollView>

I have gone to the Activity with the relWends first, and it didin't help. I tried switching RL to LL & to ViewGroup, but no joy. Any thoughts, everyone?




Aucun commentaire:

Enregistrer un commentaire