我正在學習使用Firebase,並開始創建我可以在Firebase控制檯中看到的用戶和密碼。但是,createUserWithEmailAndPassword方法不起作用,我不知道爲什麼,也許是Gradle的一個問題。 下面是主要代碼:Android:createUserWithEmailAndPassword不起作用(firebase)
public class MainActivity extends AppCompatActivity
{
DialogProgress dialogProgress;
EditText editEmail;
EditText editPassword;
String email;
String password;
FirebaseAuth firebaseAuth;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editEmail = (EditText) findViewById(R.id.editEmail);
editPassword = (EditText) findViewById(R.id.editPassword);
firebaseAuth = FirebaseAuth.getInstance();
findViewById(R.id.registrazione).setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
email = editEmail.getText().toString();
password = editPassword.getText().toString();
if(TextUtils.isEmpty(email))
{
Toast.makeText(getApplicationContext(),"Email vuota",Toast.LENGTH_SHORT).show();
}
if(TextUtils.isEmpty(password))
{
Toast.makeText(getApplicationContext(),"Password vuota",Toast.LENGTH_SHORT).show();
}
if(!(TextUtils.isEmpty(email)||TextUtils.isEmpty(password)))
{
dialogProgress = new DialogProgress();
dialogProgress.show(getSupportFragmentManager().beginTransaction(),"Dialog");
firebaseAuth.createUserWithEmailAndPassword(email,password).addOnCompleteListener(new OnCompleteListener<AuthResult>()
{
@Override
public void onComplete(@NonNull Task<AuthResult> task)
{
if(task.isSuccessful())
{
Toast.makeText(getApplicationContext(),"Registrazione avvenuta",Toast.LENGTH_SHORT).show();
getSupportFragmentManager().beginTransaction().remove(dialogProgress);
}
else
{
Toast.makeText(getApplicationContext(), "Registrazione fallita", Toast.LENGTH_SHORT).show();
getSupportFragmentManager().beginTransaction().remove(dialogProgress);
}
}
});
}
}
});
}
}
這裏是我的gradle這個項目:
buildscript
{
repositories
{
jcenter()
}
dependencies
{
classpath 'com.android.tools.build:gradle:2.2.3'
classpath 'com.google.gms:google-services:3.0.0'
}
}
allprojects
{
repositories
{
jcenter()
}
}
task clean(type: Delete)
{
delete rootProject.buildDir
}
這裏是我的gradle這個應用程序:
apply plugin: 'com.android.application'
android
{
compileSdkVersion 25
buildToolsVersion "25.0.2"
defaultConfig
{
applicationId "com.example.utente.myfirebase"
minSdkVersion 15
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner
"android.support.test.runner.AndroidJUnitRunner"
}
buildTypes
{
release
{
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-
rules.pro'
}
}
}
dependencies
{
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2',
{
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.1.0'
testCompile 'junit:junit:4.12'
compile 'com.google.firebase:firebase-auth:10.0.1'
}
apply plugin: 'com.google.gms.google-services'
當你說的方法不叫,你的意思是永遠不會到達的onComplete回調?或者,createUserWithEmailAndPassword永遠不會到達。你可以通過在createUserWithEmailAndPassword之外加一個斷點來檢查你的調試器,看看它是否停在那裏。 – wnieves19
@ snowman28924該方法被調用,但它不起作用。 task.isSuccessful()始終爲false – Curio
然後調用else語句'task.getException()' –