简体   繁体   English

无法在Android Studio中识别sensorEvent

[英]sensorEvent is not recognized in android studio

I'm trying to make a compass in a fragment for android. 我正在尝试为Android片段制作一个指南针。 I followed some courses online and read the documentation, but i'm getting the error as said in the title. 我在线上学习了一些课程,并阅读了文档,但标题中却出现了错误。

my code is this: 我的代码是这样的:

 public void onSensorChanged(SensorEvent event) {
    final float alpha = 0.97f;

    synchronized (this){

        if(sensorEvent.sensor.getType()  == Sensor.TYPE_ACCELEROMETER){
            mGravity[0] = alpha * mGravity[0] + (1 - alpha) * sensorEvent.values[0];
            mGravity[1] = alpha * mGravity[1] + (1 - alpha) * sensorEvent.values[1];
            mGravity[2] = alpha * mGravity[2] + (1 - alpha) * sensorEvent.values[2];


        }
        if(sensorEvent.sensor.getType()  == Sensor.TYPE_MAGNETIC_FIELD){
            mGeomagnetic[0] = alpha * mGeomagnetic[0] + (1 - alpha) * sensorEvent.values[0];
            mGeomagnetic[1] = alpha * mGeomagnetic[1] + (1 - alpha) * sensorEvent.values[1];
            mGeomagnetic[2] = alpha * mGeomagnetic[2] + (1 - alpha) * sensorEvent.values[2];
        }

        float R[] = new float[9];
        float I[] = new float[9];
        boolean succes = SensorManager.getRotationMatrix(R,I, mGravity, mGeomagnetic);

        if(succes){
            float orientation[] = new float[3];
            SensorManager.getOrientation(R, orientation);
            azimuth = (float) Math.toDegrees(orientation[0]);
            azimuth = (azimuth + 360) % 360;

            Animation animation = new RotateAnimation(-correctAzimuth, azimuth, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
            correctAzimuth = azimuth;
            animation.setDuration(500);
            animation.setFillAfter(true);
            imageBoussole.startAnimation(animation);
        }

    }
}

in the synchronized bloc the sensorEven is not recognized by android studio. 在同步块中,即使是android studio也无法识别sensorEven。 Here are my imports: 这是我的进口商品:

import android.content.Context; 
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;

The first occurrence of sensorEvent in your code is here: 代码中的sensorEvent首次出现在这里:

if(sensorEvent.sensor.getType()  == Sensor.TYPE_ACCELEROMETER){

sensorEvent is not defined anywhere in the code in your listing. 在清单中代码的任何地方都没有定义sensorEvent It is not surprising that Android Studio does not know what to do with it. 毫不奇怪,Android Studio不知道如何处理它。

You named the parameter to your method event , not sensorEvent : 您将参数命名为方法event ,而不是sensorEvent

public void onSensorChanged(SensorEvent event) {

So, change that to: 因此,将其更改为:

public void onSensorChanged(SensorEvent sensorEvent) {

Or, change all references to sensorEvent to be event . 或者, sensorEvent所有引用更改为event The method parameter and your uses of that parameter need to match. 方法参数和您对该参数的使用需要匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android Studio无法识别字体 - Fonts are not recognized at Android Studio 在 Android Studio 中无法识别 createFromAsset() - createFromAsset() is not recognized in Android Studio android studio,按钮对象无法识别, - android studio, button object not recognized, 被android studio识别但不能蚀的android设备? - android device recognized by android studio but not eclipse? android studio'javah'无法识别为内部或外部命令 - android studio 'javah' is not recognized as an internal or external command Android Studio编译器无法识别GraphicOverlay类 - GraphicOverlay class is't recognized by android studio compiler 如何显示 Android 的 sensorevent.timestamp 的准确时间? - How to display the exactly time from sensorevent.timestamp for Android? 使用Gradle版本2.2.3在android studio项目中无法识别方法testCompile? - Method testCompile not recognized in android studio project using Gradle version 2.2.3? Android Studio Terminal错误:“ keytool”未被识别为内部或外部命令,可操作程序或批处理文件 - Android Studio Terminal ERROR:'keytool' is not recognized as an internal or external command, operable program or batch file Android 清单中无法识别活动 - Activity Not recognized in Android Manifest
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM