gpt4 book ai didi

android - 使用电子邮件和密码进行 Firebase 身份验证

转载 作者:行者123 更新时间:2023-11-29 23:21:29 25 4
gpt4 key购买 nike

我对 Android Studio 和 Firebase 也比较陌生。我已完成设置并连接到 Firebase,但此错误不断弹出,导致应用程序崩溃:

 --------- beginning of crash
2019-01-17 14:38:11.420 10795-10795/com.example.asus.cab E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.asus.cab, PID: 10795
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.asus.cab/com.example.asus.cab.DriverLoginRegisterActivity}: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.example.asus.cab. Make sure to call FirebaseApp.initializeApp(Context) first.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2902)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3037)
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:1797)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6642)
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.asus.cab. Make sure to call FirebaseApp.initializeApp(Context) first.
at com.google.firebase.FirebaseApp.getInstance(com.google.firebase:firebase-common@@16.0.4:240)
at com.google.firebase.auth.FirebaseAuth.getInstance(Unknown Source:1)
at com.example.asus.cab.DriverLoginRegisterActivity.onCreate(DriverLoginRegisterActivity.java:45)
at android.app.Activity.performCreate(Activity.java:7131)
at android.app.Activity.performCreate(Activity.java:7122)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2882)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3037) 
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:1797) 
at android.os.Handler.dispatchMessage(Handler.java:106) 
at android.os.Looper.loop(Looper.java:193) 
at android.app.ActivityThread.main(ActivityThread.java:6642) 
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) 

I have also Initialize Firebase Auth like so, mAuth = FirebaseAuth.getInstance(); but I still get the same error

I also did included the dependencies classpath 'com.google.gms:google- services:4.1.0'

and plugins apply plugin: 'com.google.gms.google-services' which are mentioned in most solutions, but nothing seems to work

This is the following code and according to the logcat, the error in line 48 points to mAuth = FirebaseAuth.getInstance();

public class DriverLoginRegisterActivity extends AppCompatActivity {

private Button DriverLoginButton;
private Button DriverRegisterButton;
private TextView DriverRegisterLink;
private TextView DriverStatus;
private EditText EmailDriver;
private EditText PasswordDriver;
private ProgressDialog loadingBar;
private FirebaseAuth mAuth;

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

DriverLoginButton = (Button) findViewById(R.id.driver_login_btn);
DriverRegisterButton = (Button) findViewById(R.id.driver_register_btn);
DriverRegisterLink = (TextView) findViewById(R.id.register_driver_link);
DriverStatus = (TextView) findViewById(R.id.driver_status);
EmailDriver = (EditText) findViewById(R.id.email_driver);
PasswordDriver = (EditText) findViewById(R.id.password_driver);
loadingBar = new ProgressDialog(this);

// Initialize Firebase Auth
mAuth = FirebaseAuth.getInstance();

DriverRegisterButton.setVisibility(View.INVISIBLE);
DriverRegisterButton.setEnabled(false);

DriverRegisterLink.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
DriverLoginButton.setVisibility(View.INVISIBLE);
DriverRegisterLink.setVisibility(View.INVISIBLE);
DriverStatus.setText("Register Driver");
DriverRegisterButton.setVisibility(View.VISIBLE);
DriverLoginButton.setEnabled(true);
}
});

DriverRegisterButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view)
{
String email = EmailDriver.getText().toString();
String password = PasswordDriver.getText().toString();

RegisterDriver(email, password);
}
});


}

private void RegisterDriver(String email, String password) {
if (TextUtils.isEmpty(email)) {
Toast.makeText(DriverLoginRegisterActivity.this, "Please Enter Email..", Toast.LENGTH_SHORT).show();

}

if (TextUtils.isEmpty(password)) {
Toast.makeText(DriverLoginRegisterActivity.this, "Please Enter Password..", Toast.LENGTH_SHORT).show();

} else {
loadingBar.setTitle("Driver Registration");
loadingBar.setMessage("Please wait...");
loadingBar.show();


mAuth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
Toast.makeText(DriverLoginRegisterActivity.this, "Driver Register Succesful..", Toast.LENGTH_SHORT).show();
loadingBar.dismiss();
} else {
Toast.makeText(DriverLoginRegisterActivity.this, "Registration Unsuccessful, Please Try Again..", Toast.LENGTH_SHORT).show();
loadingBar.dismiss();
}
}
});
}
}
}

最佳答案

确保您遵循 firebase 文档的每一步:
Firebase documentation on auth
您需要将 google 服务添加到您的项目级 gradle 文件中:

classpath 'com.google.gms:google-services:4.2.0'

并且您需要将 firebase 核心依赖项添加到您的应用级 gradle 文件中:

implementation 'com.google.firebase:firebase-core:16.0.6'

确保您在登录方法下的控制台中启用了身份验证选项

关于android - 使用电子邮件和密码进行 Firebase 身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54230549/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com