I have a problem with using radioButtons. I want to send a frame via bluetooth, and add a prefix to the frame by checking one of the radioButtons. I have no problems with sending frame without a prefix or prefix added permanently on the code. There is the function to send:
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");
}
radioButtons in MainActivity:
RadioButton radioRTR;
RadioButton radioMonitorFMASK;
RadioButton radioMonitorFID;
RadioButton radioFID;
RadioButton radioFMASK;
In 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);
When I'm trying to call sendFrame(), the application stop working. Where am I wrong? Programming in Android Studio 2.2.3.
Message from stacktrace:
--------- 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$4.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)
Unfortunely I can't debug the app, because android studio can't see my device. I can debug only on virtual machine.
This is the error
In 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);
Make it as follows in the onCreate() Method:
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);
You have created local variables in the onCreate() Method and bound the radioButtons to those instead of the global variables.