在getChildCount上始终返回null(NPE)-检查所有内容

时间:2018-07-13 00:24:26

标签: java android for-loop android-relativelayout android-checkbox

好的,所以我在 layout 目录中的不同XML文件中有多个RelativeLayouts。在Java MainActivity.class中,每个ID均已正确标识,正确声明和正确初始化。这些XML文件由include标记使用。我想取消选中每个XML文件中每个RelativeLayout中的所有CheckBox。我使用以下代码(这是RL之一):

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

我的问题是无论我检查什么,Android Studio告诉我rWen没有孩子。 NPE表示getChildCount返回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>

我首先使用relWends参加了活动,但并没有帮助。我尝试将RL切换为LL和ViewGroup,但没有任何乐趣。大家有什么想法吗?

0 个答案:

没有答案
相关问题