I have an issue registering a second listener for a 2nd radio group.
XML:
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="124dp"
android:layout_height="123dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.014">
<RadioButton
android:id="@+id/WhiteLight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/whiteLed"
android:textColor="@color/colorWhite" />
<RadioButton
android:id="@+id/BlueLight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/blueLed"
android:textColor="@color/colorWhite" />
<RadioButton
android:id="@+id/RedLight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/redLed"
android:textColor="@color/colorWhite" />
<RadioButton
android:id="@+id/InfraredLight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/irLed"
android:textColor="@color/colorWhite" />
<RadioButton
android:id="@+id/OffLight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:checked="true"
android:text="@string/offLed"
android:textColor="@color/colorWhite" />
</RadioGroup>
<SeekBar
android:id="@+id/seekBar5"
style="@style/Widget.AppCompat.SeekBar.Discrete"
android:layout_width="258dp"
android:layout_height="67dp"
android:layout_marginStart="8dp"
android:layout_marginTop="56dp"
android:layout_marginEnd="8dp"
android:max="20"
android:progress="0"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toEndOf="@+id/radioGroup"
app:layout_constraintTop_toTopOf="parent" />
<RadioGroup
android:layout_width="210dp"
android:layout_height="120dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.043"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/seekBar5"
app:layout_constraintVertical_bias="0.112">
<RadioButton
android:id="@+id/HDMI1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:checked="true"
android:text="@string/Camera1"
android:textColor="@color/colorWhite" />
<RadioButton
android:id="@+id/HDMI2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/Camera2"
android:textColor="@color/colorWhite" />
<RadioButton
android:id="@+id/HDMI3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/Camera3"
android:textColor="@color/colorWhite" />
<RadioButton
android:id="@+id/HDMI4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/Camera4"
android:textColor="@color/colorWhite" />
</RadioGroup>
Java (copied from onCreate()):
final RadioGroup LEDGroup = findViewById(R.id.radioGroup);
final SeekBar LEDIntensity = findViewById((R.id.seekBar5));
final RadioGroup CameraGroup = findViewById((R.id.cameraGroup)) ;
LEDGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// checkedId is the RadioButton selected
//LEDIntensity.setProgress (2);
Toast.makeText(MainActivity.this, "This is LED select",
Toast.LENGTH_LONG).show();
//send selected LED to USB port where checkedId is teh selected LED
}
});
CameraGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// checkedId is the RadioButton selected
Toast.makeText(MainActivity.this, "This is Camera select",
Toast.LENGTH_LONG).show();
//send selected camera to USB port where checkId is the selected camera
}
});
When I start debugging the code, I get a NullPointerException on the CamerGroup.setOnCheckedChangeListener line. When I comment out that section of code, my app starts up properly.
Is it actually possible to have 2 separate onCheckedChanged registered, or do I have to make one listener and figure out which RadioGroup is calling the listener in the listener itself.
All my searches have shown examples using only one RadioGroup, and I can't seem to recall any apps that I have every used that have had more than one RadioGroup at a time.
Give your 2nd radiogroup this id:
android:id="@+id/cameraGroup"
you forgot it.
So this line:
final RadioGroup CameraGroup = findViewById((R.id.cameraGroup)) ;
since cameraGroup
can't be found makes CameraGroup
null
and CameraGroup.setOnCheckedChangeListener()
throws NPE.
And yes you can have as many listeners as the radio groups.