当前位置: 代码迷 >> java >> 单击“创建新帐户”时,应用程序崩溃
  详细解决方案

单击“创建新帐户”时,应用程序崩溃

热度:52   发布时间:2023-07-26 14:23:51.0

每当我点击“创建新帐户”按钮时,应用就会崩溃。 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)

在创建任何活动之前,也许在应用程序级别初始化Firebase:

<application
    android:name="MyApp"
    [...]

public class MyApp extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        FirebaseApp.initializeApp(this);
    }
}

也许你拼错了什么。 错误说:

FirebaseApp.initializeApp(this) 

在描述中说明FirebaseApp.initialize(this);

确保添加了这一行-在应用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);
 }
}

如果您不知道如何制作应用程序类及其用法,那么本篇可能会对您有所帮助。

  相关解决方案