如何正确使用单选按钮?
How to use radio buttons properly?
我在使用单选按钮时遇到问题。我想通过蓝牙发送一个帧,并通过检查其中一个单选按钮为帧添加前缀。发送没有前缀或在代码上永久添加前缀的帧没有问题。有发送功能:
void sendFrame()throws IOException{
if (radioRTR.isChecked()){
mmOutputStream.write(170); // 0xAA hex
}
else if (radioMonitorFMASK.isChecked()){
mmOutputStream.write(187); // 0xBB hex
}
else if (radioMonitorFMASK.isChecked()) {
mmOutputStream.write(204); // 0xCC hex
}
else if (radioFMASK.isChecked()) {
mmOutputStream.write(221); // 0xDD hex
}
else if (radioFID.isChecked()) {
mmOutputStream.write(238); // 0xEE hex
}
mmOutputStream.write(170); // 0xAA hex - prefix added permanently, only to check
mmOutputStream.write(hexToDec(numer_filtra.getText().toString()));
mmOutputStream.write(hexToDec(ID.getText().toString()));
mmOutputStream.write(hexToDec(filtr_ID.getText().toString()));
mmOutputStream.write(hexToDec(filtr_maska.getText().toString()));
myLabel.setText("Frame sent");
}
MainActivity 中的单选按钮:
RadioButton radioRTR;
RadioButton radioMonitorFMASK;
RadioButton radioMonitorFID;
RadioButton radioFID;
RadioButton radioFMASK;
在 OnCreate() 中:
RadioButton radioRTR = (RadioButton)findViewById(R.id.radioRTR);
RadioButton radioMonitorFMASK = (RadioButton)findViewById(R.id.radioMonitorFMASK);
RadioButton radioMonitorFID = (RadioButton)findViewById(R.id.radioMonitorFID);
RadioButton radioFMASK = (RadioButton)findViewById(R.id.radioFMASK);
RadioButton radioFID = (RadioButton)findViewById(R.id.radioFID);
当我尝试调用 sendFrame() 时,应用程序停止工作。我哪里错了?
在 Android Studio 2.2.3.
中编程
来自堆栈跟踪的消息:
--------- beginning of crash
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.gieorgij.interfejsnowy, PID: 3171
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.widget.RadioButton.isChecked()' on a null object reference
at com.example.gieorgij.interfejsnowy.MainActivity.sendFrame(MainActivity.java:307)
at com.example.gieorgij.interfejsnowy.MainActivity.onClick(MainActivity.java:152)
at android.view.View.performClick(View.java:5610)
at android.view.View$PerformClick.run(View.java:22265)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
很遗憾,我无法调试该应用程序,因为 android studio 看不到我的设备。我只能在虚拟机上调试。
您的对象似乎未绑定到 xml 的单选按钮,或者您在初始化单选按钮之前调用了 sendFrames()。检查以下代码:
使这些变量成为 class:
的全局变量
private RadioButton radioRTR;
private RadioButton radioMonitorFMASK ;
private RadioButton radioMonitorFID;
private RadioButton radioFMASK;
private RadioButton radioFID;
private RadioGroup radioGroup;
在 onCreate() 中初始化这些单选按钮:
radioRTR = (RadioButton)findViewById(R.id.radioRTR);
radioMonitorFMASK = (RadioButton)findViewById(R.id.radioMonitorFMASK);
radioMonitorFID = (RadioButton)findViewById(R.id.radioMonitorFID);
radioFMASK = (RadioButton)findViewById(R.id.radioFMASK);
radioFID = (RadioButton)findViewById(R.id.radioFID);
radioGroup = (RadioGroup)findViewById(R.id.radio_group);
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
//handle your click here
}
});
sendFrames();
这是错误
在 OnCreate() 中:
RadioButton radioRTR = (RadioButton)findViewById(R.id.radioRTR);
RadioButton radioMonitorFMASK = (RadioButton)findViewById(R.id.radioMonitorFMASK);
RadioButton radioMonitorFID = (RadioButton)findViewById(R.id.radioMonitorFID);
RadioButton radioFMASK = (RadioButton)findViewById(R.id.radioFMASK);
RadioButton radioFID = (RadioButton)findViewById(R.id.radioFID);
在onCreate()方法中做如下:
radioRTR = (RadioButton)findViewById(R.id.radioRTR);
radioMonitorFMASK = (RadioButton)findViewById(R.id.radioMonitorFMASK);
radioMonitorFID = (RadioButton)findViewById(R.id.radioMonitorFID);
radioFMASK = (RadioButton)findViewById(R.id.radioFMASK);
radioFID = (RadioButton)findViewById(R.id.radioFID);
您已经在 onCreate() 方法中创建了局部变量并将 radioButtons 绑定到那些而不是全局变量。
我在使用单选按钮时遇到问题。我想通过蓝牙发送一个帧,并通过检查其中一个单选按钮为帧添加前缀。发送没有前缀或在代码上永久添加前缀的帧没有问题。有发送功能:
void sendFrame()throws IOException{
if (radioRTR.isChecked()){
mmOutputStream.write(170); // 0xAA hex
}
else if (radioMonitorFMASK.isChecked()){
mmOutputStream.write(187); // 0xBB hex
}
else if (radioMonitorFMASK.isChecked()) {
mmOutputStream.write(204); // 0xCC hex
}
else if (radioFMASK.isChecked()) {
mmOutputStream.write(221); // 0xDD hex
}
else if (radioFID.isChecked()) {
mmOutputStream.write(238); // 0xEE hex
}
mmOutputStream.write(170); // 0xAA hex - prefix added permanently, only to check
mmOutputStream.write(hexToDec(numer_filtra.getText().toString()));
mmOutputStream.write(hexToDec(ID.getText().toString()));
mmOutputStream.write(hexToDec(filtr_ID.getText().toString()));
mmOutputStream.write(hexToDec(filtr_maska.getText().toString()));
myLabel.setText("Frame sent");
}
MainActivity 中的单选按钮:
RadioButton radioRTR;
RadioButton radioMonitorFMASK;
RadioButton radioMonitorFID;
RadioButton radioFID;
RadioButton radioFMASK;
在 OnCreate() 中:
RadioButton radioRTR = (RadioButton)findViewById(R.id.radioRTR);
RadioButton radioMonitorFMASK = (RadioButton)findViewById(R.id.radioMonitorFMASK);
RadioButton radioMonitorFID = (RadioButton)findViewById(R.id.radioMonitorFID);
RadioButton radioFMASK = (RadioButton)findViewById(R.id.radioFMASK);
RadioButton radioFID = (RadioButton)findViewById(R.id.radioFID);
当我尝试调用 sendFrame() 时,应用程序停止工作。我哪里错了? 在 Android Studio 2.2.3.
中编程来自堆栈跟踪的消息:
--------- beginning of crash
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.gieorgij.interfejsnowy, PID: 3171
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.widget.RadioButton.isChecked()' on a null object reference
at com.example.gieorgij.interfejsnowy.MainActivity.sendFrame(MainActivity.java:307)
at com.example.gieorgij.interfejsnowy.MainActivity.onClick(MainActivity.java:152)
at android.view.View.performClick(View.java:5610)
at android.view.View$PerformClick.run(View.java:22265)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
很遗憾,我无法调试该应用程序,因为 android studio 看不到我的设备。我只能在虚拟机上调试。
您的对象似乎未绑定到 xml 的单选按钮,或者您在初始化单选按钮之前调用了 sendFrames()。检查以下代码: 使这些变量成为 class:
的全局变量private RadioButton radioRTR;
private RadioButton radioMonitorFMASK ;
private RadioButton radioMonitorFID;
private RadioButton radioFMASK;
private RadioButton radioFID;
private RadioGroup radioGroup;
在 onCreate() 中初始化这些单选按钮:
radioRTR = (RadioButton)findViewById(R.id.radioRTR);
radioMonitorFMASK = (RadioButton)findViewById(R.id.radioMonitorFMASK);
radioMonitorFID = (RadioButton)findViewById(R.id.radioMonitorFID);
radioFMASK = (RadioButton)findViewById(R.id.radioFMASK);
radioFID = (RadioButton)findViewById(R.id.radioFID);
radioGroup = (RadioGroup)findViewById(R.id.radio_group);
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
//handle your click here
}
});
sendFrames();
这是错误
在 OnCreate() 中:
RadioButton radioRTR = (RadioButton)findViewById(R.id.radioRTR);
RadioButton radioMonitorFMASK = (RadioButton)findViewById(R.id.radioMonitorFMASK);
RadioButton radioMonitorFID = (RadioButton)findViewById(R.id.radioMonitorFID);
RadioButton radioFMASK = (RadioButton)findViewById(R.id.radioFMASK);
RadioButton radioFID = (RadioButton)findViewById(R.id.radioFID);
在onCreate()方法中做如下:
radioRTR = (RadioButton)findViewById(R.id.radioRTR);
radioMonitorFMASK = (RadioButton)findViewById(R.id.radioMonitorFMASK);
radioMonitorFID = (RadioButton)findViewById(R.id.radioMonitorFID);
radioFMASK = (RadioButton)findViewById(R.id.radioFMASK);
radioFID = (RadioButton)findViewById(R.id.radioFID);
您已经在 onCreate() 方法中创建了局部变量并将 radioButtons 绑定到那些而不是全局变量。