2016-10-17 4 views
-1

私は緯度と経度の保存された値にFirebaseのデータベースを使用し、問題はそれが私にこのエラーを送っている:ERROR: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

あなたは2つのエラーを説明しました:あなたのタイトルに1つ、質問の本文に1つのエラーがあります。それは " – EJP

答えて

0

lat = String.valueOf(location.getLatitude());lon = String.valueOf(location.getLongitude());で使用し、あなたのlocationオブジェクトはnullのようです。

より良い分析のために、このクラス全体のコードで質問を編集してください。

関連する問題