2017-04-25 89 views
0

目前我通過藍牙模塊從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) 

回答

0

調度後,你可以運行你刪除的StringBuilder的內容:

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

這意味着可運行的內部StringBuilder.substring通話不在範圍內的緩衝區。 (它的大小爲0)

+0

好的,那麼你能幫我解決這個問題嗎? –

+0

我需要該行,以便每次都可以獲取更新的字符串值。如果我不會刪除更新的sensor0值將如何存儲? –

相關問題