【问题标题】:ERROR: java.lang.IllegalArgumentException: invalid listener: null [duplicate]错误:java.lang.IllegalArgumentException:无效的侦听器:null [重复]
【发布时间】:2017-02-25 22:04:55
【问题描述】:

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

【问题讨论】:

  • 您提到了两个错误:一个在您的标题中,一个在您的问题正文中。是什么”

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


【解决方案1】:

据我了解,您在lat = String.valueOf(location.getLatitude());lon = String.valueOf(location.getLongitude()); 使用的location 对象似乎是null

为了更好的分析,我要求您使用整个课程的代码来编辑您的问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-30
    • 2020-09-16
    • 2011-06-03
    • 1970-01-01
    • 2017-09-01
    • 2016-01-26
    • 1970-01-01
    相关资源
    最近更新 更多