我试图在延迟一段时间后执行应用程序崩溃

时间:2017-04-25 16:56:58

标签: java android exception bluetooth

目前我通过蓝牙模块从arduino接收传感器值,并在android屏幕上不断观察它。但是我希望在值为“。”时执行某些代码。并且至少持续7秒。我使用postDelayed方法在7秒后再次获取并检查sensor0变量的值。如果它仍然相同,它应该执行某些代码。但应用程序在特定延迟后崩溃,错误代码为 java.lang.StringIndexOutOfBoundsException

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();                            //get length of data received
                        txtStringLength.setText("String Length = " + String.valueOf(dataLength));

                        if (recDataString.charAt(0) == '#')                                //if it starts with # we know it is what we are looking for
                        {
                            sensor0 = recDataString.substring(1, 2);             //get sensor value from string between indices 1-5

                            sensorView0.setText(" What We Got Is:  " + sensor0 + "");//update the textviews with sensor values

                            if (sensor0.equals(".")) {
                                Toast.makeText(getApplicationContext(), "Value of sensor0 is: " + sensor0, Toast.LENGTH_SHORT).show();
//                                Intent i = new Intent(MainActivity.this, UltimateActivity.class);
//                                startActivity(i);
                                final Handler handler = new Handler();
                                handler.postDelayed(new Runnable() {
                                    @Override
                                    public void run() {
                                        // Do something after 7s = 7000ms
                                        sensor0 = recDataString.substring(1, 2);
                                        if (sensor0.equals(".")){
                                            Toast.makeText(getApplicationContext(), "Value matched... ", Toast.LENGTH_SHORT).show();
                                        }
                                        else{
                                            Toast.makeText(getApplicationContext(), "Current value of sensor0 is: " + sensor0, Toast.LENGTH_SHORT).show();
                                        }
                                    }
                                }, 7000);
                            }

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

我认为postDelayed方法中的sensor0变量找不到字符串值。但我确实找不到错误在哪里。

这是logcat:

04-25 22:25:17.938 31584-31584/com.example.sparker.bluetoothintegrate E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                        Process: com.example.sparker.bluetoothintegrate, PID: 31584
                                                                                        java.lang.StringIndexOutOfBoundsException: length=0; regionStart=1; regionLength=1
                                                                                            at java.lang.AbstractStringBuilder.startEndAndLength(AbstractStringBuilder.java:211)
                                                                                            at java.lang.AbstractStringBuilder.substring(AbstractStringBuilder.java:616)
                                                                                            at java.lang.StringBuilder.substring(StringBuilder.java)
                                                                                            at com.example.sparker.bluetoothintegrate.MainActivity$1$1.run(MainActivity.java:93)
                                                                                            at android.os.Handler.handleCallback(Handler.java:815)
                                                                                            at android.os.Handler.dispatchMessage(Handler.java:104)
                                                                                            at android.os.Looper.loop(Looper.java:207)
                                                                                            at android.app.ActivityThread.main(ActivityThread.java:5769)
                                                                                            at java.lang.reflect.Method.invoke(Native Method)
                                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
                                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)

1 个答案:

答案 0 :(得分:0)

安排runnable后,删除StringBuilder的内容:

recDataString.delete(0, recDataString.length()); //clear all string data

这意味着对Runnable内部StringBuilder.substring的调用不在缓冲区的范围内。 (大小为0)