将字符串转换为int或boolean inside handler

时间:2015-04-25 15:22:33

标签: java android handler

我正在尝试构建引擎监视器,我通过蓝牙从arduino接收一串数据。我创建了bluetoothIn处理程序,它读取并将字符串拆分为变量。我需要将变量从字符串转换为整数或双精度,但在处理程序内执行此操作会导致应用程序崩溃。我知道处理程序返回这就是应用程序崩溃的原因。那么如何从处理程序中提取数据?

bluetoothIn = new Handler() {
  public void handleMessage(android.os.Message msg) {
    if (msg.what == handlerState) {                                     //if message is what we want
      String readMessage = (String) msg.obj;                                                                // msg.arg1 = bytes from connect thread
      recDataString.append(readMessage);                                      //keep appending to string until ~
      int endOfLineIndex = recDataString.indexOf("~");                    // determine the end-of-line
      if (endOfLineIndex > 0) {                                           // make sure there data before ~
        String dataInPrint = recDataString.substring(0, endOfLineIndex);    // extract string
        txtString.setText("Data Received = " + dataInPrint);
        int dataLength = dataInPrint.length();
        txtStringLength.setText("String Length = " + String.valueOf(dataLength));

        if (recDataString.charAt(0) == '#') {
          //datapro=String.valueOf(recDataString);
          //Message msg = Message.obtain();
          //msg.obj=datapro;
          //msg.setTarget(Handler2);
          //msg.sendToTarget();
          sensor0 = recDataString.substring(1, 5);             //get sensor value from string between indices 1-5
          sensor1 = recDataString.substring(6, 10);
          sensor2 = recDataString.substring(11, 15);
          sensor3 = recDataString.substring(16, 20);

          RPM = (Double.valueOf(sensor2) * 1000);
          V = ((RPM / ratio[box]) / FD) * (0.118);
          status = Integer.valueOf(sensor3);

          sensorView0.setText(" Battery Voltage = " + sensor0 + "V");    //update the textviews with sensor values
          sensorView1.setText(" Velocity = " + String.valueOf(V));
          sensorView2.setText("RPM=" + String.valueOf(RPM));
          sensorView3.setText(String.valueOf(status));

        }

        recDataString.delete(0, recDataString.length());                    //clear all string data
        // strIncom =" ";
        dataInPrint = " ";
      }
    }
  }
};

logcat的:

04-26 17:07:51.288: I/art(13924): Late-enabling -Xcheck:jni
04-26 17:07:51.413: D/BluetoothAdapter(13924): 72305841: getState() :  mService = null. Returning STATE_OFF
04-26 17:07:51.426: D/OpenGLRenderer(13924): Use EGL_SWAP_BEHAVIOR_PRESERVED: true
04-26 17:07:51.435: D/Atlas(13924): Validating map...
04-26 17:07:51.500: I/Adreno-EGL(13924): <qeglDrvAPI_eglInitialize:379>: QUALCOMM Build: 01/15/15, ab0075f, Id3510ff6dc
04-26 17:07:51.501: I/OpenGLRenderer(13924): Initialized EGL, version 1.4
04-26 17:07:51.521: D/OpenGLRenderer(13924): Enabling debug mode 0
04-26 17:08:45.253: W/BluetoothAdapter(15820): getBluetoothService() called with no BluetoothManagerCallback
04-26 17:08:51.273: D/AndroidRuntime(15820): Shutting down VM
04-26 17:08:51.273: D/AndroidRuntime(15820): --------- beginning of crash
04-26 17:08:51.275: E/AndroidRuntime(15820): FATAL EXCEPTION: main
04-26 17:08:51.275: E/AndroidRuntime(15820): Process: com.example.alpha_version, PID: 15820
04-26 17:08:51.275: E/AndroidRuntime(15820): java.lang.NumberFormatException: Invalid int: "0.00"
04-26 17:08:51.275: E/AndroidRuntime(15820):    at java.lang.Integer.invalidInt(Integer.java:138)
04-26 17:08:51.275: E/AndroidRuntime(15820):    at java.lang.Integer.parse(Integer.java:410)
04-26 17:08:51.275: E/AndroidRuntime(15820):    at java.lang.Integer.parseInt(Integer.java:367)
04-26 17:08:51.275: E/AndroidRuntime(15820):    at java.lang.Integer.parseInt(Integer.java:334)
04-26 17:08:51.275: E/AndroidRuntime(15820):    at java.lang.Integer.valueOf(Integer.java:525)
04-26 17:08:51.275: E/AndroidRuntime(15820):    at com.example.alpha_version.Main$1.handleMessage(Main.java:95)
04-26 17:08:51.275: E/AndroidRuntime(15820):    at android.os.Handler.dispatchMessage(Handler.java:102)
04-26 17:08:51.275: E/AndroidRuntime(15820):    at android.os.Looper.loop(Looper.java:135)
04-26 17:08:51.275: E/AndroidRuntime(15820):    at android.app.ActivityThread.main(ActivityThread.java:5254)
04-26 17:08:51.275: E/AndroidRuntime(15820):    at java.lang.reflect.Method.invoke(Native Method)
04-26 17:08:51.275: E/AndroidRuntime(15820):    at java.lang.reflect.Method.invoke(Method.java:372)
04-26 17:08:51.275: E/AndroidRuntime(15820):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
04-26 17:08:51.275: E/AndroidRuntime(15820):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
04-26 17:08:57.542: I/Process(15820): Sending signal. PID: 15820 SIG: 9
04-26 17:08:57.942: D/OpenGLRenderer(15987): Use EGL_SWAP_BEHAVIOR_PRESERVED: true
04-26 17:08:57.948: D/Atlas(15987): Validating map...
04-26 17:08:58.010: I/Adreno-EGL(15987): <qeglDrvAPI_eglInitialize:379>: QUALCOMM Build: 01/15/15, ab0075f, Id3510ff6dc
04-26 17:08:58.012: I/OpenGLRenderer(15987): Initialized EGL, version 1.4
04-26 17:08:58.066: D/OpenGLRenderer(15987): Enabling debug mode 0

2 个答案:

答案 0 :(得分:0)

您的c我想这是您status = Integer.valueOf(sensor3);中的第95行。此语句抛出异常,因为Main.java的值为sensor3,显然不是int

答案 1 :(得分:0)

你不需要这个:

status = Integer.valueOf(sensor3); 

我还删除了一些您不需要的String.valueOf

新代码:

bluetoothIn = new Handler() {
  public void handleMessage(android.os.Message msg) {
    if (msg.what == handlerState) {                                     //if message is what we want
      String readMessage = (String) msg.obj;                                                                // msg.arg1 = bytes from connect thread
      recDataString.append(readMessage);                                      //keep appending to string until ~
      int endOfLineIndex = recDataString.indexOf("~");                    // determine the end-of-line
      if (endOfLineIndex > 0) {                                           // make sure there data before ~
        String dataInPrint = recDataString.substring(0, endOfLineIndex);    // extract string
        txtString.setText("Data Received = " + dataInPrint);
        int dataLength = dataInPrint.length();
        txtStringLength.setText("String Length = " + String.valueOf(dataLength)); // <-- removed String.valueOf

        if (recDataString.charAt(0) == '#') {
          //datapro=String.valueOf(recDataString);
          //Message msg = Message.obtain();
          //msg.obj=datapro;
          //msg.setTarget(Handler2);
          //msg.sendToTarget();
          sensor0 = recDataString.substring(1, 5);             //get sensor value from string between indices 1-5
          sensor1 = recDataString.substring(6, 10);
          sensor2 = recDataString.substring(11, 15);
          sensor3 = recDataString.substring(16, 20);

          RPM = (Double.valueOf(sensor2) * 1000);
          V = ((RPM / ratio[box]) / FD) * (0.118);
          // status = Integer.valueOf(sensor3);  // <-- you do not need this

          sensorView0.setText(" Battery Voltage = " + sensor0 + "V");    //update the textviews with sensor values
          sensorView1.setText(" Velocity = " + V); // <-- removed String.valueOf
          sensorView2.setText("RPM=" + RPM); // <-- removed String.valueOf
          sensorView3.setText("Status " + status); // <-- removed String.valueOf

        }

        recDataString.delete(0, recDataString.length());                    //clear all string data
        // strIncom =" ";
        dataInPrint = " ";
      }
    }
  }
};