无法解析方法'registerListenercom.xxx.xxx.MainActivity,android.hardware.SensorManager,int'

如何解决无法解析方法'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 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?