每当我单击“创建新帐户”按钮时,应用程序就会崩溃。 Logcat中的错误指出我需要调用FirebaseApp.initialize(this);我已经完成,但仍然发生相同的错误。这是我的RegisterActivity。
private FirebaseAuth mAuth;
private ProgressDialog loadingbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
FirebaseApp.initializeApp(this);
mAuth = FirebaseAuth.getInstance();
我的Logcat在下面。
Process: com.example.whatsappclone, PID: 9246
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.whatsappclone/com.example.whatsappclone.RegisterActivity}: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.example.whatsappclone. Make sure to call FirebaseApp.initializeApp(Context) first.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.example.whatsappclone. Make sure to call FirebaseApp.initializeApp(Context) first.
at com.google.firebase.FirebaseApp.getInstance(com.google.firebase:firebase-common@@16.0.2:240)
at com.google.firebase.auth.FirebaseAuth.getInstance(Unknown Source:1)
at com.example.whatsappclone.RegisterActivity.onCreate(RegisterActivity.java:36)
at android.app.Activity.performCreate(Activity.java:7136)
at android.app.Activity.performCreate(Activity.java:7127)
答案 0 :(得分:1)
也许在创建任何活动之前在应用程序级别初始化Firebase:
<application
android:name="MyApp"
[...]
public class MyApp extends Application {
@Override
public void onCreate() {
super.onCreate();
FirebaseApp.initializeApp(this);
}
}
答案 1 :(得分:0)
也许您拼错了什么。错误提示:
FirebaseApp.initializeApp(this)
虽然您正在描述FirebaseApp.initialize(this);
答案 2 :(得分:0)
确保您添加了这一行-在应用gradle的末尾应用插件:“ com.google.gms.google-services”。
dependencies {
....
}
apply plugin: 'com.google.gms.google-services'
然后创建一个扩展Application类的子类,并在那里创建此Firebase实例。
public class MyApp extends Application {
@Override
public void onCreate() {
super.onCreate();
FirebaseApp.initializeApp(this);
}
}
如果您不知道如何制作应用程序类及其用法,此post可能会对您有所帮助。