【问题标题】:Firebase App crash when I open it initializeFirebase 应用程序在我打开它时崩溃初始化
【发布时间】:2019-02-07 00:32:37
【问题描述】:

当我添加这个

        demoRef = FirebaseDatabase.getInstance().getReference().child("txt");

我的应用程序崩溃,当我删除它时,应用程序可以工作,有人可以帮助我。

我尝试了很多方法,比如更改数据库的版本,但没有任何效果,我花了 3 多周的时间没有任何解决

这是我的主要活动

公共类 MainActivity 扩展 Activity {

private HashMap <String,Object>map=new HashMap();
private Button send;
private EditText edit;
private DatabaseReference demoRef;

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

    FirebaseApp.initializeApp(this);
    demoRef = FirebaseDatabase.getInstance().getReference().child("txt");

    send=findViewById(R.id.mainButton1);
    edit=findViewById(R.id.mainEditText1);


}

}

这是我的构建

dependencies {
    //compile 'com.google.firebase:firebase-database:+'

    compile fileTree(dir: 'libs', include: ['*.jar'])

    compile 'com.google.firebase:firebase-database:16.0.1'
    compile 'com.google.firebase:firebase-core:16.0.1'



}
apply plugin: 'com.google.gms.google-services'

这是我的 Logcat

FATAL EXCEPTION: main
Process: com.firebase, PID: 5291
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.firebase/com.firebase.MainActivity}: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.firebase. Make sure to call FirebaseApp.initializeApp(Context) first.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2724)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2789)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1527)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:203)
at android.app.ActivityThread.main(ActivityThread.java:6251)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1063)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:924)
Caused by: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.firebase. Make sure to call FirebaseApp.initializeApp(Context) first.
at com.google.firebase.FirebaseApp.getInstance(SourceFile:218)
at com.google.firebase.database.FirebaseDatabase.getInstance(Unknown Source)
at com.firebase.MainActivity.onCreate(MainActivity.java:27)
at android.app.Activity.performCreate(Activity.java:6677)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2677)
... 9 more
Shutting down VM
FATAL EXCEPTION: main
Process: com.firebase, PID: 8039
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.firebase/com.firebase.MainActivity}: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.firebase. Make sure to call FirebaseApp.initializeApp(Context) first.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2724)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2789)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1527)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:203)
at android.app.ActivityThread.main(ActivityThread.java:6251)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1063)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:924)
Caused by: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.firebase. Make sure to call FirebaseApp.initializeApp(Context) first.
at com.google.firebase.FirebaseApp.getInstance(SourceFile:218)
at com.google.firebase.database.FirebaseDatabase.getInstance(Unknown Source)
at com.firebase.MainActivity.onCreate(MainActivity.java:27)
at android.app.Activity.performCreate(Activity.java:6677)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2677)
... 9 more

【问题讨论】:

    标签: java android-studio firebase-realtime-database aide-ide


    【解决方案1】:

    删除

    FirebaseApp.initializeApp(this);
    

    然后让引用像这样

    private HashMap <String,Object>map=new HashMap();
    private Button send;
    private EditText edit;
    private DatabaseReference demoRef;
    
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    
        demoRef = FirebaseDatabase.getInstance().getReference().child("txt");
    
        send=findViewById(R.id.mainButton1);
        edit=findViewById(R.id.mainEditText1);
    
    
    }
    

    并确保您的清单中有 INTERNET 权限

    【讨论】:

    • 你能分享你的崩溃日志吗?
    • 它显示相同的东西并要求我初始化firebase
    【解决方案2】:

    MainActivity 中删除FirebaseApp.initializeApp(this); 并将其放入您应用的Application 类的onCreate 中。 Look here.

    顺便问一下,您正在测试的 API LEVEL 是什么?从棉花糖开始 您需要为您的应用添加运行时权限。并且您可能需要 WRITE_STORAGE 权限 才能使用本地数据库。

    希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-22
      • 2023-03-18
      • 2020-06-23
      • 2015-08-30
      相关资源
      最近更新 更多