0

我是 Android 编程的新手(刚开始我的期末项目),我手头的当前任务遇到了麻烦。我正在尝试访问我的 Sphero 的 IMU 并将数据输出到文本文件中。在我学习了如何创建一个 android 应用程序并使用从https://github.com/orbotix/Sphero-Android-SDK获得的示例之后,将数据流式传输到我的 android 设备上并不是什么大问题。

但是,我一直被困在我似乎无法解决的文本阶段的输出数据上。我设法弄清楚如何使用 DataOutputStream 和其他输出流方法(缓冲区、字节等)为字符串消息和数组输出文本文件。但是,当涉及从 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 错误。有人可以请教。提前感谢您,我将非常感谢您的任何建议。

4

1 回答 1

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();
        }
    }

这就是我要分享的全部内容,希望对您有所帮助。

于 2016-04-03T06:07:30.083 回答