0

我在为指南针应用设置目标位置时遇到问题。基本上它指向一个设定的目的地(而不是北方)。但是,每当我尝试执行 destinationObj.setLatittude() 或 setLongitutde() 时,我已经计算出了所有的数学运算,应用程序强制关闭。

奇怪的是,如果我将destinationObj set.Lat/Long 放在位置监听器中,它会覆盖GPS 位置,就好像应用程序只能识别一个位置而不是两个。

请提供任何指导。

谢谢,

这是我的代码:

import android.app.Activity;
...

public class CompassActivity extends Activity {

LocationManager locationManager;
Location LocationObj, destinationObj;

    private SensorEventListener mListener = new SensorEventListener() {
        public void onSensorChanged( SensorEvent event ) {

            if ( LocationObj == null ) return;

            ...bearing calculation...

                float bearing = LocationObj.bearingTo(destinationObj);

            rotateImageView( needle, R.drawable.needle, direction);

        }

        public void onAccuracyChanged(Sensor sensor, int accuracy) {
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        needle = (ImageView)findViewById(R.id.needle);
        log = (TextView)findViewById(R.id.log);
        lat = (TextView)findViewById(R.id.lat);

        **destinationObj.setLatitude(20);
        destinationObj.setLongitude(30);**

        LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        LocationListener locationListener = new LocationListener() {
            public void onLocationChanged(Location location) {
              LocationObj = location;
              log.setText(Double.toString(location.getLongitude()));
              lat.setText(Double.toString(location.getLatitude()));
            }
            public void onStatusChanged(String provider, int status, Bundle extras) {}
            public void onProviderEnabled(String provider) {}
            public void onProviderDisabled(String provider) {}
          };

        mlocManager.requestLocationUpdates( LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);

        mSensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
        mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
    }

    private void rotateImageView( ImageView imageView, int drawable, float rotate ) {
       ...image rotation...
    }


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

    @Override
    protected void onStop()
    {
        mSensorManager.unregisterListener(mListener);
        super.onStop();
    }

}
4

1 回答 1

0

因为destinationObj 仍然没有初始化并且为空

在这条线之前..

 destinationObj.setLatitude(20);

 destinationOnj=new Location(NETWORK_PROVIDER);
于 2012-04-15T07:39:31.250 回答