Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/185.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 空对象引用上的“void android.widget.Switch.setOnCheckedChangeListener(android.widget.CompoundButton$OnCheckedChangeListener)”?_Java_Android_Intellij Idea_Switch Statement - Fatal编程技术网

Java 空对象引用上的“void android.widget.Switch.setOnCheckedChangeListener(android.widget.CompoundButton$OnCheckedChangeListener)”?

Java 空对象引用上的“void android.widget.Switch.setOnCheckedChangeListener(android.widget.CompoundButton$OnCheckedChangeListener)”?,java,android,intellij-idea,switch-statement,Java,Android,Intellij Idea,Switch Statement,我在java类中的开关上有一个错误,我不知道该怎么办 错误: OnCreate方法中java类中的代码: 具有开关的xml: //我在java类中初始化的其他开关与xml中switchStudent的格式相同 空对象引用上的void android.widget.Switch.setOnCheckedChangeListener.widget.CompoundButton$OnCheckedChangeListener'表示什么?表示您正在使用空对象引用注册setOnCheckedChangeL

我在java类中的开关上有一个错误,我不知道该怎么办

错误:

OnCreate方法中java类中的代码:

具有开关的xml:

//我在java类中初始化的其他开关与xml中switchStudent的格式相同


空对象引用上的void android.widget.Switch.setOnCheckedChangeListener.widget.CompoundButton$OnCheckedChangeListener'表示什么?

表示您正在使用空对象引用注册setOnCheckedChangeListener。
在调用setOnCheckedChangeListener之前,请检查Swich或sGrade是否为null。

在设置setOnCheckedChangeListener之前,应该向交换机添加null检查,如下所示

Switch s = (Switch) findViewById(R.id.SwitchID);

 if (s != null) {
   s.setOnCheckedChangeListener(this);
 }
这可能会解决你的问题

        final Switch sAllStudents = findViewById(R.id.switchStudent);
        final Switch sGrade = findViewById(R.id.switchGrade);
        final Switch sBlock = findViewById(R.id.switchBlock);
        final Switch sHouse = findViewById(R.id.switchHouse);
        final TextView student = findViewById(R.id.txtIndStudent);
        final TextView grade = findViewById(R.id.txtindGrade);
        final TextView block = findViewById(R.id.txtIndBlock);
        final TextView house = findViewById(R.id.txtIndHouse);

        sAllStudents.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (sAllStudents.isChecked()){
                    sGrade.setEnabled(true);
                    sBlock.setEnabled(true);
                    sHouse.setEnabled(true);
                    student.setEnabled(true);
                } else {
                    sGrade.setEnabled(false);
                    sBlock.setEnabled(false);
                    sHouse.setEnabled(false);
                    student.setEnabled(false);
                }
            }
        });

        sGrade.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (sGrade.isChecked()){
                    grade.setEnabled(true);
                } else {
                    grade.setEnabled(false);
                }
            }
        }); //the same format for sBlock and sHouse


<Switch
            android:checked="false"
            android:text="All SHS Students"
            android:textSize="18sp"
            android:paddingLeft="20dp"
            android:paddingRight="180dp"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:id="@+id/switchStudent"
            android:background="@drawable/border"
            android:backgroundTint="#80ffffff" app:layout_constraintTop_toTopOf="parent"
            android:layout_marginTop="60dp"/>
Switch s = (Switch) findViewById(R.id.SwitchID);

 if (s != null) {
   s.setOnCheckedChangeListener(this);
 }