I am trying to build a tracker app using Google Maps API and Firebase. I am trying to use the getValue() method in onStart() to take values from Firebase and use them as latitude and longitude to display location. My code:
MapsActivity.java
private FirebaseDatabase firebaseDatabase=FirebaseDatabase.getInstance();
private DatabaseReference mRootReference =firebaseDatabase.getReference();
private DatabaseReference mChildReference=mRootReference.child("Raunak Trikha");
private DatabaseReference mChildReference1=mChildReference.child("id");
private DatabaseReference mChildReference2=mChildReference1.child("lat");
private DatabaseReference mChildReference3=mChildReference1.child("long");
@Override
protected void onStart(){
super.onStart();
mChildReference2.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
rlat=dataSnapshot.getValue(double.class);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}`
Whenever I try to run this app in my AVD it crashes.
You have to call
FirebaseApp.initializeApp(getApplicationContext());
line, when your app started?
At onCreate()
method of first activity or application class.
To simplify your code, please use the following lines:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference latRef = rootRef.child("Raunak Trikha").child("id").child("lat");
DatabaseReference longRef = rootRef.child("Raunak Trikha").child("id").child("long");
latRef.addListenerForSingleValueEvent(/* ... */);
longRef.addListenerForSingleValueEvent(/* ... */);
All this lines are needed in onCreate()
method.
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.