[英]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.