2012-04-15 6 views
0

У меня возникли проблемы с настройкой места назначения для приложения компаса. В основном это указывает на заданное место назначения (вместо северного). Тем не менее, у меня есть все математические соображения, когда я пытаюсь сделать destinationObj.setLatittude() или setLongitutde(), сила приложения закрывается.Местоположение Android

Странная вещь, если я помещаю 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(); 
    } 

} 

ответ

0

Поскольку destinationObj является stioll не инициализирован и является нулевым

До этой линии ..

destinationObj.setLatitude(20); 

поставил

destinationOnj=new Location(NETWORK_PROVIDER); 
+0

ничего себе, это было просто. Благодаря! Фактически используется destinationObj = новое местоположение (""). – mgalal

Смежные вопросы