2016-10-17 3 views
-1

Я использую База данных Firebase сохраненных значений широты и долготы, проблема в том, что отправить мне эту ошибку:ОШИБКА: java.lang.IllegalArgumentException: неверный слушатель: нулевой

Attempt to invoke virtual method 'double android.location.Location.getLatitude()' on a null object reference 

Я попытался с используемыми в классе для методов onLocationChanged, но присылайте ту же ошибку.

Это мой код:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    //Context Android 
    setContentView(R.layout.activity_); 
    Firebase.setAndroidContext(this); 
    //This class is to connect with firebase 
    Firebase referencia = new Firebase(Path.URL_FIREBASE); 

    lat = String.valueOf(location.getLatitude()); 
    lon = String.valueOf(location.getLongitude()); 

    LocationClass ll = new LocationClass(); 
    ll.setLatitud(lat); 
    ll.setLongitud(lon); 


    Firebase NuevaReferencia = referencia.child("Location").push(); 
    NuevaReferencia.setValue(ll); 




    locationManager = (LocationManager) getSystemService(android.content.Context.LOCATION_SERVICE); 

    if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.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; 
    } 
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this.locationListener); 




    NuevaReferencia.addValueEventListener(new ValueEventListener() { 
     @Override 
     public void onDataChange(DataSnapshot dataSnapshot) { 
      for (DataSnapshot dataSnapshot1: dataSnapshot.getChildren()) { 

       LocationClass LL = dataSnapshot.getValue(LocationClass.class); 
       String string = "Longitude:"+LL.getLongituded()+"Latitude"+LL.getLatituded(); 
      } 
     } 

     @Override 
     public void onCancelled(FirebaseError firebaseError) { 
      System.out.println("The read failed: " + firebaseError.getMessage()); 
     } 
    }); 
+0

Вы упомянули две ошибки: одну в своем названии и одну в теле вашего вопроса. Что он" – EJP

ответ

0

Из того, что я могу понять, ваш location объект, используемый в lat = String.valueOf(location.getLatitude()); и lon = String.valueOf(location.getLongitude());, кажется null.

Для лучшего анализа прошу вас отредактировать свой вопрос с кодом для всего этого класса.

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