2016-08-18 31 views
0

我目前正在試圖通過與微控制器板通過藍牙通信接收的數據。 每個數據傳輸(每200毫秒)發送一個由4個字符組成的字符串(4位數字)給我的android設備,我可以用每次有新數據更新的textView顯示值。這在MainActivity中發生了10秒。用androidplot繪製字符串數組中的數據

爲了得到我想要繪製從我保存每個字符串的字符串列表這樣的數據的數組:

// Create Array List to send to xyPlot activity 
List<String> incomingStringData = new ArrayList<>(); 

// more code happening... 

String loadCellString = recDataString.substring(1, 5); // get sensor value from string between indices 1-5 
incomingStringData.add(loadCellString); // Adding each incoming substring to List<String> incomingStringData 

// more code happening 

// On button click send data to xyPlot activity 
btnPlot.setOnClickListener(new View.OnClickListener() { 
    public void onClick(View v) { 

    Intent xyPlotScreen = new Intent(getApplicationContext(), xyPLot.class); 

    //Sending data to another Activity 
    String[] xyPlotStringArray = incomingStringData.toArray(new String[0]); 
    xyPlotScreen.putExtra("string-array", xyPlotStringArray); 

    // Start plotscreen (xyPlot) activity 
    startActivity(xyPlotScreen); 
    } 
} 

這個數據IST發送到活動xyPlot(這是我從簡單xyPlot複製例如androidplot的,感謝BTW)在那裏處理是這樣的:

// Get String Array from Motor Control (MainActivity): 
Intent xyPlotScreen = getIntent(); 
String[] thrustStringArray = xyPlotScreen.getStringArrayExtra("string-array"); 

// Convert String-Array into an Integer to be able to plot: 
String[] parts = thrustStringArray[0].split(","); 
Integer[] intThrust = new Integer[parts.length]; 

for(int n = 0; n < parts.length; n++) { 
    intThrust[n] = Integer.parseInt(parts[n]); 
} 

// Create the Number series for plotting 
Number[] thrustSeries = intThrust; 

// Turn the above arrays into XYSeries': 
// (Y_VALS_ONLY means use the element index as the x value 
XYSeries thrustSeries = new SimpleXYSeries(Arrays.asList(thrustArray), 
      SimpleXYSeries.ArrayFormat.XY_VALS_INTERLEAVED,"Thrust"); 

現在我用XY_VALS_INTERLEAVED只知道,如果我可以積傳入的數據我得到,即使它沒有意義(以後我的字符串將由一個時間戳組成x軸)。

數據類型「Number」(它也是一個Integer的權利??)當然不支持String數組。所以我做了從String到整數的轉換,然後我可以構建應用程序。

當我開始情節活動我得到此錯誤消息:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.controlcenter.controlcenter/com.controlcenter.controlcenter.xyPLot}: java.lang.IndexOutOfBoundsException: Cannot auto-generate series from odd-sized xy List. 

即使當我改變數據傳入我的機器人裝置上的頻率(每秒1倍的值,所以10個值)予得到相同的錯誤信息。我猜這個問題出現在我將字符串轉換爲Integer的地方。但我無法想出一種方法來做這種轉換的正確方式,以繪製一個Number[]類型的數據的傳感器數據。

希望你們能幫助我。

在此先感謝! Chris

回答

0

您看到的錯誤僅僅是說您傳遞給Androidplot的數據數組具有奇數個元素,在使用交錯模式時這是非法的:交錯意味着您正在傳遞x/y值順序。如果此數組的大小爲奇數,則表示缺少x或y分量。

你可以通過很多方法解決這個問題。可能最容易做的事情就是修改您的for循環以忽略來自讀數的最終值(如果其奇數):

int partsLen = parts.length; 
    if(partsLen < 2) { 
     // do something to gracefully avoid continuing as theres not enough data to plot. 
    } else { 
     if(partsLen % 2 == 1) { 
      // if the size of the input is odd, ignore the last element to make it even: 
      partsLen--; 
     } 
    } 
    for(int n = 0; n < partsLen; n++) { 
     intThrust[n] = Integer.parseInt(parts[n]); 
    } 
+0

感謝您的回覆。我包括你的代碼snipet。雖然現在我得到這個錯誤信息:java.lang.RuntimeException:無法啓動活動ComponentInfo {com.controlcenter.controlcenter/com.controlcenter.controlcenter.xyPLot}:java.lang.NumberFormatException:Invalid int:「70 「' – hohmchri

+0

可能是您正在解析爲數字的數據已損壞,或者解析它的方式不正確。 – Nick