如何解决无法解析方法'registerListenercom.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,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,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();
}
我的应用程序应显示指向指南针等目的地的方向。用户将给出纬度和经度。 或者,也许您对如何使用指南针进行定位有不同的想法
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。