我正在学习使用Firebase,并开始创建我可以在Firebase控制台中看到的用户和密码。但是,方法createUserWithEmailAndPassword不起作用,我不知道为什么,可能是Gradle的一个问题。 这是主要代码:
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'