I'amは、GoogleのFirebase認証オプションを使用して、私のAndroidアプリにユーザーを登録しようとするが、私はエラーを取得し、私のToast
私は考えるToast.makeText(RegistrationActivity.this, "Sign Up Error!", Toast.LENGTH_SHORT).show();
Firebase認証登録エラーが
このコード行で私のエラーを処理していますが発生しましたSHA-1とfirebaseの接続が正しくリンクされました。 Firebaseコンソールの画像添付ファイル
私はここに私のソースコードとxmlファイルを追加します。
package com.example.anu.activityone;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
public class RegistrationActivity extends AppCompatActivity {
private EditText txtEmail, txtPassword;
private Button btnRegister;
private FirebaseAuth mAuth;
/*
public abstract class FirebaseAuth extends Object
The entry point of the Firebase Authentication SDK.
*/
private FirebaseAuth.AuthStateListener FirebaseAuthStateListener;
/* FirebaseAuth.AuthStateListener is a interface,
that Listener called when there is a change in the authentication state.
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_registration);
// method called when user logged in or logged out
mAuth = FirebaseAuth.getInstance();
FirebaseAuthStateListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
final FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if(user != null){
Intent intent = new Intent(RegistrationActivity.this, MainActivity.class);
startActivity(intent);
finish();
return;
}
}
};
txtEmail = (EditText)findViewById(R.id.email);
txtPassword = (EditText)findViewById(R.id.password);
btnRegister = (Button)findViewById(R.id.register);
// Register a new user
btnRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
final String email = txtEmail.getText().toString();
final String password = txtPassword.getText().toString();
/*
Support User Registration
With password-based authentication, new users must register themselves by providing
a unique email address and a password. To add this functionality to your app,
you can use the createUserWithEmailAndPassword() method of the FirebaseAuth class.
As its name suggests, the method expects an email address and a password as its arguments.
*/
mAuth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(RegistrationActivity.this, new OnCompleteListener<AuthResult>() {
/*
public interface OnCompleteListener
Listener called when a Task completes.
*/
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if(!task.isSuccessful()){
Toast.makeText(RegistrationActivity.this, "Sign Up Error!", Toast.LENGTH_SHORT).show();
}
}
});
/*
To be able to determine the result of the createUserWithEmailAndPassword() method,
you must add an OnCompleteListener to it using the addOnCompleteListener() method.
*/
}
});
}
// To start Firebase authentication
@Override
protected void onStart() {
super.onStart();
mAuth.addAuthStateListener(FirebaseAuthStateListener);
}
// To stop Firebase authentication when app pause
@Override
protected void onStop() {
super.onStop();
mAuth.removeAuthStateListener(FirebaseAuthStateListener);
}
}
XMLファイル
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.anu.activityone.RegistrationActivity"
android:orientation="vertical">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="email"
android:id="@+id/email"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="password"
android:id="@+id/password"/>
<Button
android:id="@+id/register"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Registration" />
</LinearLayout>
あなたのToastを更新して、失敗の理由を表示します。 Add: 'task.getException()。getMessage()' –
これはうまくいきました。私はエラーが見つかりました。 tnx –