简体   繁体   中英

Android Studio app stops when location is accessed from fire base

The current activity page accepts userID from previous Activity and searches the corresponding user and mark his location on the map.

There are no compile errors. App crashes whenever I access this activity in mobile.

public class map extends FragmentActivity implements OnMapReadyCallback {
private String UserID;
private GoogleMap mMap;
private FirebaseFirestore db = FirebaseFirestore.getInstance();
private studetails stu;

//private LocationListener Locationlistener;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_map);
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    UserID = getIntent().getExtras().getString("UserId");
    DocumentReference doc = db.collection("Userdetails").document(UserID);
    doc.get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
        @Override
        public void onSuccess(DocumentSnapshot documentSnapshot) {
            stu = documentSnapshot.toObject(studetails.class);
        }
    });

    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);

    //ActivityCompat.requestPermissions( activity: this,new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, PackageManager.PERMISSION_GRANTED);
    //ActivityCompat.requestPermissions( activity: this,new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, PackageManager.PERMISSION_GRANTED);
}


@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
    Location loc;
    loc = stu.getlocation();
    // Add a marker in Sydney and move the camera
    LatLng sydney = new LatLng(loc.getLatitude(), loc.getLongitude());
    mMap.addMarker(new MarkerOptions().position(sydney).title("Busstop"));
    mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}

}

Custom Object used to push data into firestore

I'm new to android-studio, If I missed any relevant information please mention it below.

Database Structure

Part of the logfile before APP crashed

According to your log, FirebaseApp is not initialized in this process . I suspect that this line is causing your issues:

FirebaseFirestore db = FirebaseFirestore.getInstance();

As when the class instance is created, it automatically tries to get the default Firestore instance even though the FirebaseApp has not been properly setup.

Don't init db in the declaration, rather, defer to onCreate , specifically after you have called FirebaseApp.initializeApp(this)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM