FirebaseApp初始化时Firebase存储崩溃了吗?

时间:2016-10-18 10:42:21

标签: android firebase firebase-storage

我正在使用Android中的Firebase存储。我在初始化FirebaseStorage参考时遇到问题。

MyCode:

MainActivity.java

public class MainActivity extends AppCompatActivity {

private StorageReference mStorage;

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

    FirebaseStorage storage = FirebaseStorage.getInstance();  // App crash in this line
    mStorage = storage.getReferenceFromUrl("my firebase storage url");

} }

申请类

public class FirebaseApplication extends Application {

@Override
public void onCreate() {
    super.onCreate();

    Firebase.setAndroidContext(this);
    Firebase.getDefaultConfig().setPersistenceEnabled(true);
}

@Override
protected void attachBaseContext(Context base) {
    super.attachBaseContext(base);
    MultiDex.install(this);
}}

logcat的

 Caused by: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.sample.firebase.upload. Make sure to call FirebaseApp.initializeApp(Context) first.
                                                                            at com.google.firebase.FirebaseApp.getInstance(Unknown Source)
                                                                            at com.google.firebase.storage.FirebaseStorage.getInstance(Unknown Source)
                                                                            at com.sample.firebase.upload.MainActivity.onCreate(MainActivity.java:34)
                                                                            at android.app.Activity.performCreate(Activity.java:6100)
                                                                            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1112)
                                                                            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2468)
                                                                            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2601) 
                                                                            at android.app.ActivityThread.access$800(ActivityThread.java:178) 
                                                                            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1470) 
                                                                            at android.os.Handler.dispatchMessage(Handler.java:111) 
                                                                            at android.os.Looper.loop(Looper.java:194) 
                                                                            at android.app.ActivityThread.main(ActivityThread.java:5637) 
                                                                            at java.lang.reflect.Method.invoke(Native Method) 
                                                                            at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960) 
                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 

应用程序在MainActivity中崩溃了。 Log说,Firebase没有在应用程序中初始化,但我已经在应用程序类中进行了初始化。

请帮帮我,建议我解决一下。

2 个答案:

答案 0 :(得分:2)

请确保您已在Gradle(应用级别)中添加了依赖项compile 'com.google.firebase:firebase-storage:11.0.4'

如果仍然存在,则面临同样的问题。

  

错误: com.google.firebase.storage.FirebaseStorage.getInstance(未知来源)

您可能正在使用Firebase身份验证。该问题将通过使 firebase-auth firebase-storage 的版本相同来解决。

compile 'com.google.firebase:firebase-storage:11.0.4'
compile 'com.google.firebase:firebase-auth:11.0.4'

还请确保您已在Java文件中导入语句

import com.google.firebase.storage.FirebaseStorage;

答案 1 :(得分:1)

我确实认为你还没有完成Firebase上的Android入门步骤:

https://firebase.google.com/docs/android/setup

作为此过程的一部分,您需要将json配置文件添加到应用程序中。设置完所有内容后,Android SDK会自动为您初始化。