[英]Firebase Authentication With Email & Password
I am relatively new to Android Studio and Firebase as well.我对 Android Studio 和 Firebase 也比较陌生。 I have done the setup and connected to Firebase, but this error keeps popping out, causing the app to crash:
我已完成设置并连接到 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();
我也像这样初始化 Firebase Auth,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'
我也确实包含了依赖
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和插件
apply plugin: 'com.google.gms.google-services'
在大多数解决方案中都提到过,但似乎没有任何效果This is the following code and according to the logcat, the error in line 48 points to mAuth = FirebaseAuth.getInstance();
这是以下代码,根据 logcat,第 48 行中的错误指向 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();
}
}
});
}
}
}
Make sure you follow every step of the firebase docs:确保您遵循 firebase 文档的每一步:
Firebase documentation on auth 有关身份验证的 Firebase 文档
You need to add google services to your project level gradle file:您需要将 google services 添加到您的项目级 gradle 文件中:
classpath 'com.google.gms:google-services:4.2.0'
And you need to add to your app level gradle file the firebase core dependency:并且您需要将 firebase 核心依赖项添加到您的应用程序级 gradle 文件中:
implementation 'com.google.firebase:firebase-core:16.0.6'
Make sure you enabled the auth options in the console under sign in methods确保您在登录方法下的控制台中启用了身份验证选项
Your logcat itself gives the solution to your problem:您的 logcat 本身为您的问题提供了解决方案:
You need to call FirebaseApp.initializeApp(Context) in your application class.您需要在应用程序类中调用FirebaseApp.initializeApp(Context) 。
You are getting this error because in your code, you have not initialized the firebase.您收到此错误是因为在您的代码中,您尚未初始化 Firebase。
just add this in your on create method of the activity只需将此添加到您的活动的创建方法中
FirebaseApp.initializeApp(this);
mAuth = FirebaseAuth.getInstance();
As pointed by someone earlier too, the error indicates that you've either missed invoking FirebaseApp.initializeApp(Context) method or the sequence is incorrect.正如之前有人指出的那样,该错误表明您要么错过了调用 FirebaseApp.initializeApp(Context) 方法,要么顺序不正确。
There has been recent updates in using Firebase Auth SDK and the new approach is quite simple and minimal code.最近使用 Firebase Auth SDK 进行了更新,新方法非常简单且代码最少。 Please refer here for more details https://firebase.google.com/docs/auth/android/firebaseui
请参阅此处了解更多详情https://firebase.google.com/docs/auth/android/firebaseui
This is the approach to setup Auth providers and launch auth intent.这是设置身份验证提供程序和启动身份验证意图的方法。 You would need to add separate logic for Callaback handling and UI customization (if applicable) that too is available on above mentioned docs link.
您需要为回调处理和 UI 自定义(如果适用)添加单独的逻辑,这些逻辑也可在上述文档链接中找到。
// Choose authentication providers
List<AuthUI.IdpConfig> providers = Arrays.asList(
new AuthUI.IdpConfig.EmailBuilder().build(),
new AuthUI.IdpConfig.PhoneBuilder().build(),
new AuthUI.IdpConfig.GoogleBuilder().build(),
new AuthUI.IdpConfig.FacebookBuilder().build(),
new AuthUI.IdpConfig.TwitterBuilder().build());
// Create and launch sign-in intent
startActivityForResult(
AuthUI.getInstance()
.createSignInIntentBuilder()
.setAvailableProviders(providers)
.build(),
RC_SIGN_IN);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.