在为第二个广播组注册第二个侦听器时出现问题。
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(从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
}
});
当我开始调试代码时,在CamerGroup.setOnCheckedChangeListener行上收到NullPointerException。当我注释掉那部分代码时,我的应用程序正确启动。
实际上是否可以有两个单独的onCheckedChanged注册,还是我必须创建一个侦听器并弄清楚哪个RadioGroup在侦听器本身中调用该侦听器。
我所有的搜索都显示了仅使用一个RadioGroup的示例,而且我似乎想不起我曾经使用过的任何应用程序一次具有多个RadioGroup。
答案 0 :(得分:1)
为您的第二个广播组提供此ID:
android:id="@+id/cameraGroup"
您忘记了。
所以这行:
final RadioGroup CameraGroup = findViewById((R.id.cameraGroup)) ;
由于找不到cameraGroup
,使得CameraGroup
null
而CameraGroup.setOnCheckedChangeListener()
则抛出NPE。
是的,您可以拥有与广播组一样多的听众。