java - Android - 传感器或监听器为空

标签 java android android-sensors

我正在尝试在我的应用程序中实现传感器监听器,但它在 mSensorManager.registerListener(sensorEventListener, mSensor, mSensorManager.SENSOR_DELAY_FASTEST); 处抛出 sensor orlistener is null 错误 行。这是我的完整代码:

    dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));*/
    mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_GAME_ROTATION_VECTOR);
    sensorEventListener = new SensorEventListener() {
        public void onSensorChanged(SensorEvent event) {
            double xAxis = event.values[0];
            TextView textView = (TextView) findViewById(R.id.rotation);
            textView.setText(String.valueOf(xAxis));
        }

        public void onAccuracyChanged(Sensor s, int i) {

        }
    };

    mSensorManager.registerListener(sensorEventListener, mSensor, mSensorManager.SENSOR_DELAY_FASTEST);

我检查了一下,传感器实际上是null。我究竟做错了什么?谢谢。

编辑:该设备是第二代 Moto G。

最佳答案

原因是设备不支持 Sensor.TYPE_GAME_ROTATION_VECTOR。因此传感器为零。

关于java - Android - 传感器或监听器为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27803288/

相关文章:

java - 向 JAX-B 生成的元素添加静态属性

java - 如何使用 Android 更新 Firestore 中的数组元素?

android - 使用SearchView时如何叠加Activity内容?

android - Activity 在 onClick 方法中泄漏了窗口

Android:如何调用其他 API 级别中存在的方法?

Android加速度计传感器监听器频率意外变化

android - 银河 Nexus : Sensor Sampling Rate becomes faster when sampling more Sensors

java - 使用 SQL Server 2005/2008 的最新 jdbc 驱动程序时,准备好的语句、 View 和存储过程的性能比较如何?

java - 如何管理打开的数据报套接字以避免超过最大值?

android - Amazon Cognito 和 Android SDK 的问题 NotAuthorizedException : Invalid login token