FirebaseApp初始化时Firebase存储崩溃? [英] Firebase Storage crash on FirebaseApp initialization?
问题描述
我正在研究Android中的Firebase存储.初始化FirebaseStorage参考时遇到问题.
I am working on Firebase Storage in Android. I am facing issue while initializing the FirebaseStorage reference.
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中崩溃.日志说,Firebase尚未在应用程序中初始化,但我已经在应用程序类中进行了初始化.
The application is getting crashed in the MainActivity. Log says , Firebase is not initialized in the app but i have already initialize in the application class.
请帮帮我,给我一些解决方法.
Please help me out , suggest me some solution.
推荐答案
请确保已在Gradle(应用程序级别)中添加了依赖项compile 'com.google.firebase:firebase-storage:11.0.4'
Please make sure that you've added the dependency compile 'com.google.firebase:firebase-storage:11.0.4'
in Gradle (app level)
如果仍然如此,您将面临同样的问题.
If still, you face the same issue.
错误:com.google.firebase.storage.FirebaseStorage.getInstance(未知来源)
Error: com.google.firebase.storage.FirebaseStorage.getInstance(Unknown Source)
您可能正在使用Firebase身份验证.通过使 firebase-auth 和 firebase-storage 的版本相同可以解决该问题.
You may be using the Firebase Authentication. The issue will be resolved by making the version of firebase-auth and firebase-storage same.
compile 'com.google.firebase:firebase-storage:11.0.4'
compile 'com.google.firebase:firebase-auth:11.0.4'
还请确保已在Java文件中导入语句
Please also make sure you've import statement in Java file
import com.google.firebase.storage.FirebaseStorage;
这篇关于FirebaseApp初始化时Firebase存储崩溃?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!