0

コンパスアプリの宛先の場所を設定するのに問題があります。基本的には、(北ではなく)設定された目的地を指します。私はすべての計算を理解しましたが、destinationObj.setLatittude()またはsetLongitutde()を実行しようとすると、アプリの強制が終了します。

奇妙なことに、destinationObj set.Lat / Longをロケーションリスナーに配置すると、GPSロケーションが上書きされ、アプリが2つではなく1つのロケーションしか認識できないかのようになります。

任意のガイダンスをお願いします。

ありがとう、

これが私のコードです:

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 はまだ初期化されておらず、null であるため

この行の前に..

 destinationObj.setLatitude(20);

置く

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