我在“警报”对话框中创建登录表单,但在此对话框中,我想在下方添加忘记密码链接,这将链接到另一个活动。我尝试了很多修改但它崩溃了。我在这个App.App中使用Firebase身份验证,当我修改主要活动中的代码并出错时。 下面是我的主要java代码。
package com.techno.giants.technogiants;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.rengwuxian.materialedittext.MaterialEditText;
import com.techno.giants.technogiants.Model.User;
import uk.co.chrisjenx.calligraphy.CalligraphyConfig;
import uk.co.chrisjenx.calligraphy.CalligraphyContextWrapper;
public class MainActivity extends AppCompatActivity {
RelativeLayout rootLayout;
Button btnSignIn,btnRegister;
FirebaseAuth auth;
FirebaseAuth.AuthStateListener mAuthListener;
FirebaseDatabase db;
DatabaseReference users;
@Override
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
CalligraphyConfig.initDefault(new CalligraphyConfig.Builder()
.setDefaultFontPath("fonts/Arkhip_font.ttf")
.setFontAttrId(R.attr.fontPath)
.build());
setContentView(R.layout.activity_main);
//init firebase
auth = FirebaseAuth.getInstance();
db=FirebaseDatabase.getInstance();
users=db.getReference("Users");
btnRegister=(Button)findViewById(R.id.btnRegister);
btnSignIn=(Button)findViewById(R.id.btnSignIn);
rootLayout=(RelativeLayout)findViewById(R.id.rootLayout);
//Event
btnRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showRegisterDialog();
}
});
btnSignIn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showLoginDialog();
}
});
mAuthListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
if (auth.getCurrentUser() !=null){
Intent intent=new Intent(MainActivity.this,Welcome.class);
startActivity(intent);
}
}
};
}
private void showLoginDialog() {
final AlertDialog.Builder dialog=new AlertDialog.Builder(this);
dialog.setTitle("SIGN IN");
dialog.setMessage("Please use Email to Sign In");
LayoutInflater inflater=LayoutInflater.from(this);
View login_layout=inflater.inflate(R.layout.layout_login,null);
final MaterialEditText edtEmail=login_layout.findViewById(R.id.edtEmail);
final MaterialEditText edtPassword=login_layout.findViewById(R.id.edtPassword);
dialog.setView(login_layout);
//set button
dialog.setPositiveButton("SIGN IN", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
//check validation
if (TextUtils.isEmpty(edtEmail.getText().toString())) {
Snackbar.make(rootLayout, "Please enter Email Address", Snackbar.LENGTH_SHORT)
.show();
return;
}
if (TextUtils.isEmpty(edtPassword.getText().toString())) {
Snackbar.make(rootLayout, "Please enter Password", Snackbar.LENGTH_SHORT)
.show();
return;
}
if (edtPassword.getText().toString().length() < 6) {
Snackbar.make(rootLayout, "Password too short", Snackbar.LENGTH_SHORT)
.show();
return;
}
//login
auth.signInWithEmailAndPassword(edtEmail.getText().toString(),edtPassword.getText().toString())
.addOnSuccessListener(new OnSuccessListener<AuthResult>() {
@Override
public void onSuccess(AuthResult authResult) {
startActivity(new Intent(MainActivity.this,Welcome.class));
finish();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Snackbar.make(rootLayout,"Failed sign In"+e.getMessage(),Snackbar.LENGTH_SHORT)
.show();
}
});
}
});
dialog.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
});
dialog.show();
}
private void showRegisterDialog() {
final AlertDialog.Builder dialog=new AlertDialog.Builder(this);
dialog.setTitle("REGISTER");
dialog.setMessage("Please use Email to register");
LayoutInflater inflater=LayoutInflater.from(this);
View register_layout=inflater.inflate(R.layout.layout_register,null);
final MaterialEditText edtEmail=register_layout.findViewById(R.id.edtEmail);
final MaterialEditText edtPassword=register_layout.findViewById(R.id.edtPassword);
final MaterialEditText edtName=register_layout.findViewById(R.id.edtName);
final MaterialEditText edtPhone=register_layout.findViewById(R.id.edtPhone);
final MaterialEditText edtCollege=register_layout.findViewById(R.id.edtCollege);
dialog.setView(register_layout);
//set button
dialog.setPositiveButton("REGISTER", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
//check validation
if (TextUtils.isEmpty(edtEmail.getText().toString()))
{
Snackbar.make(rootLayout,"Please enter Email Address",Snackbar.LENGTH_SHORT)
.show();
return;
}
if (TextUtils.isEmpty(edtPhone.getText().toString()))
{
Snackbar.make(rootLayout,"Please enter Phone Number",Snackbar.LENGTH_SHORT)
.show();
return;
}
if (TextUtils.isEmpty(edtPassword.getText().toString()))
{
Snackbar.make(rootLayout,"Please enter Password",Snackbar.LENGTH_SHORT)
.show();
return;
}
if (edtPassword.getText().toString().length()<6)
{
Snackbar.make(rootLayout,"Password too short",Snackbar.LENGTH_SHORT)
.show();
return;
}
//register new user
auth.createUserWithEmailAndPassword(edtEmail.getText().toString(),edtPassword.getText().toString())
.addOnSuccessListener(new OnSuccessListener<AuthResult>() {
@Override
public void onSuccess(AuthResult authResult) {
//save user to db
User user=new User();
user.setEmail(edtEmail.getText().toString());
user.setName(edtName.getText().toString());
user.setPhone(edtPhone.getText().toString());
user.setPassword(edtPassword.getText().toString());
user.setCollege(edtCollege.getText().toString());
//use email to key
users.child(FirebaseAuth.getInstance().getCurrentUser().getUid())
.setValue(user)
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Snackbar.make(rootLayout,"Register success",Snackbar.LENGTH_SHORT)
.show();
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Snackbar.make(rootLayout,"Failed "+e.getMessage(),Snackbar.LENGTH_LONG)
.show();
}
});
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Snackbar.make(rootLayout,"Failed "+e.getMessage(),Snackbar.LENGTH_SHORT)
.show();
}
});
}
});
dialog.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
});
dialog.show();
}
@Override
protected void onStart() {
super.onStart();
auth.addAuthStateListener(mAuthListener);
}
}
答案 0 :(得分:0)
您可以输入日志文件吗?
某些特殊字符(。)未在FireBase中输入。