简体   繁体   English

FirebaseDatabase.getInstance()崩溃的应用程序

[英]FirebaseDatabase.getInstance() crashing app

I am trying to build a tracker app using Google Maps API and Firebase. 我正在尝试使用Google Maps API和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. 我正在尝试在onStart()中使用getValue()方法从Firebase中获取值,并将它们用作经度和纬度来显示位置。 My code: 我的代码:

MapsActivity.java 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. 每当我尝试在我的AVD中运行此应用程序时,它就会崩溃。

You have to call 你必须打电话

FirebaseApp.initializeApp(getApplicationContext());

line, when your app started? 行,您的应用何时启动?

At onCreate() method of first activity or application class. 在第一个活动或应用程序类的onCreate()方法中。

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. onCreate()方法中需要所有这些行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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