我尝试使用SensorManager.getOrientation(matrixR, orientationMatrix);来获取设备方向,如下所示。但是我不知道为什么SensorManager.getOrientation(matrixR, orientationMatrix);就像logcat所建议的那样会导致应用程序崩溃?我尝试省略SensorManager.getOrientation(matrixR, orientationMatrix);并且应用正常运行。请帮我解决这个问题

Java代码:

if (accelerometerValues != null && magnetometerValues != null) {
        float [] matrixR = new float[3];
        float [] matrixI = new float[3];
        boolean success = SensorManager.getRotationMatrix(matrixR, matrixI,
        accelerometerValues, magnetometerValues);
        if (success) {
            float [] orientationMatrix = new float[3];
            SensorManager.getOrientation(matrixR, orientationMatrix);
            tvAzimuthReading.setText(Float.toString(orientationMatrix[0]));
            tvPitchReading.setText(Float.toString(orientationMatrix[1]));
            tvRollReading.setText(Float.toString(orientationMatrix[2]));
        } else {
            Toast.makeText(getApplicationContext(), "No Rmatrix", Toast.LENGTH_LONG).show();
        }
    }


Logcat:

04-23 02:01:50.476: D/dalvikvm(24772): GC_FOR_ALLOC freed 144K, 21% free 10121K/12664K,
paused 11ms, total 11ms
04-23 02:01:50.481: I/dalvikvm-heap(24772): Grow heap (frag case) to 13.294MB for
1127536-byte allocation
04-23 02:01:50.491: D/dalvikvm(24772): GC_FOR_ALLOC freed 2K, 19% free 11221K/13768K,
paused 11ms, total 11ms
04-23 02:01:50.521: D/SensorManager(24772): registerListener :: create queue :: handler
= 0, name = LSM330DLC Acceleration Sensor, delay = 200000,
04-23 02:01:50.521: D/SensorManager(24772): registerListener :: handler = 1, name =
AK8963C Magnetic field Sensor, delay = 200000,
04-23 02:01:50.596: D/libEGL(24772): loaded /system/lib/egl/libEGL_mali.so
04-23 02:01:50.601: D/libEGL(24772): loaded /system/lib/egl/libGLESv1_CM_mali.so
04-23 02:01:50.601: D/libEGL(24772): loaded /system/lib/egl/libGLESv2_mali.so
04-23 02:01:50.606: E/(24772): Device driver API match
04-23 02:01:50.606: E/(24772): Device driver API version: 23
04-23 02:01:50.606: E/(24772): User space API version: 23
04-23 02:01:50.606: E/(24772): mali: REVISION=Linux-r3p2-01rel3 BUILD_DATE=Fri Nov 29
14:18:37 KST 2013
04-23 02:01:50.676: D/OpenGLRenderer(24772): Enabling debug mode 0
04-23 02:01:50.781: E/SensorManager(24772): Exception dispatching input event.
04-23 02:01:50.781: D/AndroidRuntime(24772): Shutting down VM
04-23 02:01:50.781: W/dalvikvm(24772): threadid=1: thread exiting with uncaught
exception (group=0x41c11700)
04-23 02:01:50.786: E/AndroidRuntime(24772): FATAL EXCEPTION: main
04-23 02:01:50.786: E/AndroidRuntime(24772): java.lang.ArrayIndexOutOfBoundsException:
length=3; index=5
04-23 02:01:50.786: E/AndroidRuntime(24772):    at
android.hardware.SensorManager.getOrientation(SensorManager.java:1135)
04-23 02:01:50.786: E/AndroidRuntime(24772):    at
com.example.sensortest01.SensorTest01.onSensorChanged(SensorTest01.java:122)
04-23 02:01:50.786: E/AndroidRuntime(24772):    at

最佳答案

尝试将您的matrixRmatrixI定义如下:

float [] matrixR = new float[9];
float [] matrixI = new float[9];

10-01 21:00