错误:java.lang.IllegalArgumentException:无效的侦听器:null

时间:2016-10-17 02:21:35

标签: java android android-studio firebase firebase-realtime-database

我使用Firebase数据库来保存纬度和经度值,问题是发给我这个错误:

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());
        }
    });

1 个答案:

答案 0 :(得分:0)

根据我的理解,locationlat = String.valueOf(location.getLatitude());使用的lon = String.valueOf(location.getLongitude());对象似乎是null

为了更好的分析,我请你用整个班级的代码编辑你的问题。