无法解析方法'registerListener(com.xxx.xxx.MainActivity, android.hardware.SensorManager, int)'



找不到这个问题的好答案。

@Override
protected void onResume() {
super.onResume();
mSensorManager.registerListener(MainActivity.this, mSensorManager, SensorManager.SENSOR_DELAY_GAME);
}

无法解析方法"registerListener(com.xxx.xxx.MainActivity,android.hardware.SensorManager,int(">

我不知道该怎么做

整个活动代码:

public class MainActivity extends AppCompatActivity {

private ImageView image, shield;
private float currentDegree = 0f;
private SensorManager mSensorManager;
private TextView tvHeading;
private Location location = new Location("A");
private Location target = new Location("B");
private LocationManager locationManager;
private EditText latitudeInput, longitudeInput;
public GeomagneticField geoField;
private Button setLocationBtn;
Dialog myDialog;
private SensorManager sensorManager;
private  Sensor mAccelerometer;
private float[] floatGravity = new float[3];
private float[] floatGeoMagnetic = new float[3];
private float[] floatOrientation = new float[3];
private float[] floatRotationMatrix = new float[9];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
image = (ImageView) findViewById(R.id.direction);
shield = (ImageView) findViewById(R.id.shield);
tvHeading = (TextView) findViewById(R.id.tvHeading);
setLocationBtn = (Button) findViewById(R.id.button);
myDialog = new Dialog(this);
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
//    ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
//   public void onRequestPermissionsResult(int requestCode, String[] permissions,
//                                          int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
double longitude = location.getLongitude();
double latitude = location.getLatitude();
mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);

//location.setLatitude(54.903535);
//location.setLongitude(23.979342);
target.setLatitude(54.904618);
target.setLongitude(23.978782);
SensorEventListener sensorEventListenerAccelrometer = new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent event) {
floatGravity = event.values;
SensorManager.getRotationMatrix(floatRotationMatrix, null, floatGravity, floatGeoMagnetic);
SensorManager.getOrientation(floatRotationMatrix, floatOrientation);
mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
shield.setRotation((float) (-floatOrientation[0] * 180 / 3.14159));
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
};
}

@Override
protected void onResume() {
super.onResume();
mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
protected void onPause() {
super.onPause();
//mSensorManager.unregisterListener(this); // to stop the listener and save battery
}

//@Override
public void onSensorChanged(SensorEvent event) {
// get the angle around the z-axis rotated
float degree = Math.round(event.values[0]);
degree += geoField.getDeclination();
float bearing = location.bearingTo(target);
degree = (bearing - degree) * -1;
degree = normalizeDegree(degree);
tvHeading.setText("Heading: " + Float.toString(degree) + " degrees");
// create a rotation animation (reverse turn degree degrees)
RotateAnimation ra = new RotateAnimation(
currentDegree,
-degree,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF,
0.5f);
// how long the animation will take place
ra.setDuration(210);
// set the animation after the end of the reservation status
ra.setFillAfter(true);
// Start the animation
image.startAnimation(ra);
currentDegree = -degree;
}
private float normalizeDegree(float value) {
if (value >= 0.0f && value <= 180.0f) {
return value;
} else {
return 180 + (180 + value);
}
}

public void onSensorChanged2(SensorEvent event) {
// get the angle around the z-axis rotated
float degree = Math.round(event.values[0]);
tvHeading.setText("Heading: " + Float.toString(degree) + " degrees");
// create a rotation animation (reverse turn degree degrees)
RotateAnimation ra = new RotateAnimation(
currentDegree,
-degree,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF,
0.5f);
// how long the animation will take place
ra.setDuration(210);
// set the animation after the end of the reservation status
ra.setFillAfter(true);
// Start the animation
shield.startAnimation(ra);
currentDegree = -degree;
}

public void onAccuracyChanged(Sensor sensor, int accuracy) {
// not in use
}

public void ShowPopup(View v) {
myDialog.setContentView(R.layout.popup);
TextView txtclose;
Button setBtn;
txtclose =(TextView) myDialog.findViewById(R.id.exitBtn);
setBtn =(Button) myDialog.findViewById(R.id.setBtn);
txtclose.setText("X");
latitudeInput =(EditText) myDialog.findViewById(R.id.latitudeInput);
longitudeInput =(EditText) myDialog.findViewById(R.id.longitudeInput);
txtclose.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
myDialog.dismiss();
}
});
setBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(latitudeInput == null || longitudeInput == null){
Toast.makeText(MainActivity.this, "You did not enter a coordinates", Toast.LENGTH_SHORT).show();
return;
}
else {
target.setLatitude(Double.parseDouble(latitudeInput.getText().toString()));
target.setLongitude(Double.parseDouble(longitudeInput.getText().toString()));
myDialog.dismiss();
}
}
});
myDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
myDialog.show();
}

我的应用程序应该像指南针一样显示目的地的方向。用户将给出纬度和经度。或者你对如何制作定位的指南针有不同的想法

我之前在处理加速度计时遇到过这个问题。我认为这里的问题是您没有实现SensorEventListener

像这样:

public class MainActivity extends AppCompatActivity implements SensorEventListener {
}

相关内容

最新更新