2017-10-24 9 views
0

インテントサービスを使用して特定の周期でFirebaseプロジェクトの場所を更新するAndroidアプリケーションを開発しています。しかし、私はかなり早い時期に壁に衝突したようです。私がfirebaseデータベースのルート参照をサービスに与えるとクラッシュします。firebaseデータベース参照でAndroidインテントサービスがクラッシュする

私はこのコードを使用する場合:

import android.app.IntentService; 
import android.content.Intent; 
import com.google.firebase.database.DatabaseReference; 
import com.google.firebase.database.FirebaseDatabase; 

public class LocationUpdater extends IntentService{ 

    public LocationUpdater() { 
     super("LocationUpdater"); 
    } 

    @Override 
    protected void onHandleIntent(Intent intent) { 
     synchronized (this){ 
      try{ 
       wait(2000); 
      }catch (InterruptedException e){ 
       e.printStackTrace(); 
      } 
     } 

    } 
} 

何もしません、それは結構です。しかし、この単一の行を追加すると、LocationUpdaterコンストラクタのすぐ上に

private DatabaseReference mRoot = FirebaseDatabase.getInstance().getReference(); 

が追加され、アプリケーションがクラッシュします。なぜこれが起こっているのかについてのアイデアはありますか? ありがとうございます!あなたが実際に最初の場所でそれを初期化せずにFirebaseのインスタンスを取得しようとしている

java.lang.RuntimeException: Unable to instantiate service com.mohana.cluster.LocationUpdater: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.mohana.cluster. Make sure to call FirebaseApp.initializeApp(Context) first. 
                     at android.app.ActivityThread.handleCreateService(ActivityThread.java:3168) 
                     at android.app.ActivityThread.-wrap5(ActivityThread.java) 
                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1563) 
                     at android.os.Handler.dispatchMessage(Handler.java:102) 
                     at android.os.Looper.loop(Looper.java:154) 
                     at android.app.ActivityThread.main(ActivityThread.java:6123) 
                     at java.lang.reflect.Method.invoke(Native Method) 
                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867) 
                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757) 
                    Caused by: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.mohana.cluster. Make sure to call FirebaseApp.initializeApp(Context) first. 
                     at com.google.firebase.FirebaseApp.getInstance(Unknown Source) 
                     at com.google.firebase.database.FirebaseDatabase.getInstance(Unknown Source) 
                     at com.mohana.cluster.LocationUpdater.<init>(LocationUpdater.java:11) 
                     at java.lang.Class.newInstance(Native Method) 
                     at android.app.ActivityThread.handleCreateService(ActivityThread.java:3165) 
                     at android.app.ActivityThread.-wrap5(ActivityThread.java)  
                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1563)  
                     at android.os.Handler.dispatchMessage(Handler.java:102)  
                     at android.os.Looper.loop(Looper.java:154)  
                     at android.app.ActivityThread.main(ActivityThread.java:6123)  
                     at java.lang.reflect.Method.invoke(Native Method)  
                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)  
                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)  
+0

アプリがlogcatからクラッシュすると、エラーメッセージを共有してください。 – Grimthorr

+0

logcat report please –

+0

私はそれを投稿しました –

答えて

0

これはlogcatでエラーが出力されます。 Firebaseをインスタンス化する前にFirebaseを初期化してください。

FirebaseApp.initializeApp(this);

関連する問題