0

This activity has 2 button, Start and Stop. When button start clicked, its save user location to firebase and button stop to stop it. So, I use LocationManager.requestLocationUpdates to make every 5 second user location inserted to firebase. But this makes my app crash, here is my code

package com.example.julio.firebasemaster;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

import java.util.HashMap;

public class MainActivity extends AppCompatActivity {

    TextView condition;
    Button start;
    Button stop;

    DatabaseReference mDatabase;
    DatabaseReference mChild;

    LocationManager locationManager;
    LocationListener locationListener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mDatabase = FirebaseDatabase.getInstance().getReference();
        mChild = mDatabase.child("user");

        start = (Button) findViewById(R.id.btnStart);
        stop = (Button) findViewById(R.id.btnStop);

        permission();

        start.setOnClickListener(new View.OnClickListener() {
            @SuppressWarnings("MissingPermission")
            @Override
            public void onClick(View view) {

                locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 0, locationListener);
                locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 5000, 0, locationListener);
                permission();

                locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

                locationListener = (new LocationListener() {
                    @Override
                    public void onLocationChanged(Location location) {
                        String latitude = location.getLatitude() + " ";
                        String longitude = location.getLongitude() + " ";

                        HashMap<String, String> data = new HashMap<>();
                        data.put("latitude", latitude);
                        data.put("longitude", longitude);

                        mChild.setValue(data);
                    }

                    @Override
                    public void onStatusChanged(String s, int i, Bundle bundle) {

                    }

                    @Override
                    public void onProviderEnabled(String s) {

                    }

                    @Override
                    public void onProviderDisabled(String s) {

                    }
                });
            }
        });

        stop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                locationManager.removeUpdates(locationListener);
            }
        });

    }

    private void permission() {
        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;
        }
    }    
}

The Crush occur when I click start button

I'm using firebase assistant, so there is no problem with the initialization

Help me please, my head want to explode thinking this bug :(

KENdi
  • 7,576
  • 2
  • 16
  • 31

1 Answers1

0

Add null check inside onLocationChanged

 public void onLocationChanged(Location location) {
                        if (location != null) {
                        String latitude = location.getLatitude() + " ";
                        String longitude = location.getLongitude() + " ";

                        HashMap<String, String> data = new HashMap<>();
                        data.put("latitude", latitude);
                        data.put("longitude", longitude);

                        mChild.setValue(data);
                        }
}

I hope this will help you

Zeeshan Shabbir
  • 6,704
  • 4
  • 38
  • 74