【问题标题】:How to use radio buttons properly?如何正确使用单选按钮?
【发布时间】:2017-06-23 09:24:16
【问题描述】:

我在使用单选按钮时遇到问题。我想通过蓝牙发送一个帧,并通过检查其中一个单选按钮为帧添加一个前缀。我没有在代码上永久添加前缀或前缀的情况下发送帧没有问题。有发送功能:

    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$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)

很遗憾,我无法调试应用程序,因为 android studio 看不到我的设备。我只能在虚拟机上调试。

【问题讨论】:

  • 您能提供一个堆栈跟踪吗?
  • 你的错误日志是怎么说的?
  • @TorhanBartel 编辑并添加了堆栈跟踪。告诉我它是否是你需要的。

标签: java android android-studio radio-button android-radiogroup


【解决方案1】:

这是错误

在 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() 方法中创建了局部变量,并将单选按钮绑定到那些而不是全局变量。

【讨论】:

    【解决方案2】:

    您的对象似乎没有绑定到您的 xml 单选按钮,或者您在初始化单选按钮之前调用了 sendFrames()。检查以下代码: 将这些变量设为类的全局变量:

    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();
    

    【讨论】:

      猜你喜欢
      • 2013-02-10
      • 1970-01-01
      • 2013-05-29
      • 1970-01-01
      • 1970-01-01
      • 2020-12-12
      • 1970-01-01
      • 1970-01-01
      • 2022-01-17
      相关资源
      最近更新 更多