2016-03-08 66 views
0

我是新來的Android編程(剛開始我的畢業設計的緣故)和我目前的工作遇到麻煩。我試圖訪問我的Sphero的IMU並將數據輸出到文本文件中。在我學習如何創建Android應用程序並玩弄我從https://github.com/orbotix/Sphero-Android-SDK獲得的示例之後,將數據流式傳輸到我的Android設備上並不是什麼大問題。嘗試在空對象引用調用虛擬方法的java.util.ArrayList com.orbotix.async.DeviceSensorAsyncMessage.getAsyncData()「

但是,我一直被困在輸出數據文本階段,我似乎無法解決。我設法弄清楚如何使用DataOutputStream和其他outputstream方法(緩衝區,字節等)爲字符串消息和數組輸出文本文件。但是當涉及到從Sphero加速度計流輸出數據時,由於應用程序不斷崩潰,我無法輸出任何文件:

引起:java.lang.NullPointerException:嘗試調用虛擬方法'java.util。 ArrayList的com.orbotix.async.DeviceSensorAsyncMessage.getAsyncData()」上一個空對象引用

這裏是我相信這是導致問題的輸出部分:

public void write_data() { 
    double accelX = ((DeviceSensorAsyncMessage) asyncMessage).getAsyncData().get(0).getAccelerometerData().getFilteredAcceleration().x; 
    ArrayList arrayList = new ArrayList(); 
    while (running == true) { 
     arrayList.add(accelX); 

     Toast.makeText(getApplicationContext(), "File Open", Toast.LENGTH_LONG).show(); 
     int sz = arrayList.size(); 
     for (int i = 0; i < sz; i++) { 
      try { 
       DataOutputStream dos = new DataOutputStream(new FileOutputStream(output_file, true)); 
       dos.writeUTF("" + arrayList); 
       dos.writeChars("\n"); 
      } catch (IOException e) { 
       Toast.makeText(getApplicationContext(), "Unable to copy data", Toast.LENGTH_LONG).show(); 
       //e.printStackTrace(); 
      } 
     } 
     try { 
      dos.close(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
} 

我不知道作爲一個事實,輸出方法是錯誤的,因爲我仍然不理解過去問題中的NullPointException錯誤。有人可以請建議。預先感謝你,我真的很感激任何建議。

回答

0

對於那些偶然尋求相同答案的人來說,我不擅長編碼的解釋,因爲我是新人,但我設法通過重組行動來解決問題。

這是新的代碼,我寫了最後工作,我期望:

public void handleAsyncMessage(AsyncMessage asyncMessage, Robot robot) { 
    if(asyncMessage == null) 
     return; 

    //Check the asyncMessage type to see if it is a DeviceSensor message 
    if(asyncMessage instanceof DeviceSensorAsyncMessage) { 
     DeviceSensorAsyncMessage message = (DeviceSensorAsyncMessage) asyncMessage; 

     if(message.getAsyncData() == null 
       || message.getAsyncData().isEmpty() 
       || message.getAsyncData().get(0) == null) 
      return; 

     //Retrieve DeviceSensorsData from the async message 
     DeviceSensorsData data = message.getAsyncData().get(0); 

     //Extract the accelerometer data from the sensor data 
     displayAccelerometer(data.getAccelerometerData()); 

     //Extract attitude data (yaw, roll, pitch) from the sensor data 
     displayAttitude(data.getAttitudeData()); 

     //Extract gyroscope data from the sensor data 
     displayGyroscope(data.getGyroData()); 

     writeAccelerometer(data.getAccelerometerData()); 
    } 
} 


public void writeAccelerometer(AccelerometerData accelerometer) { 
    if (running == true) { 
     try { 
      dos.writeBytes(String.format("%.4f", accelerometer.getFilteredAcceleration().x)); 
      dos.writeChars("\n"); 
      dos.writeBytes(String.format("%.4f", accelerometer.getFilteredAcceleration().y)); 
      dos.writeChars("\n"); 
      dos.writeBytes(String.format("%.4f", accelerometer.getFilteredAcceleration().z)); 
      dos.writeChars("\n"); 
      System.out.println("Write Successful"); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 

以上就是我給大家分享,希望它有助於。

相關問題