按下注册按钮时应用程序崩溃
Posted
技术标签:
【中文标题】按下注册按钮时应用程序崩溃【英文标题】:App crashes when registration button is pressed 【发布时间】:2018-07-06 16:21:59 【问题描述】:我对 android studio 和 java 有点陌生,但对编程不熟悉。我正在开发一个类似于 Uber 的应用程序。
当我第一次编写登录和注册活动代码时,一切都很顺利。现在,在完成应用程序的大部分功能后,辞职方法会使应用程序崩溃。我已经尝试了一切,包括获取一个新的 JSON 文件。
任何帮助将不胜感激。
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;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
public class CustomerLoginActivity extends AppCompatActivity
private EditText mEmail, mPassword;
private Button mLogin, mRegistration;
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_customer_login);
mAuth = FirebaseAuth.getInstance();
mAuthListener =[enter image description here][1] new FirebaseAuth.AuthStateListener()
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth)
//store info of current user
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if(user!=null)
Intent intent = new Intent(CustomerLoginActivity.this, CustomerMapActivity.class);
startActivity(intent);
finish();
;
mEmail = (EditText) findViewById(R.id.email);
mPassword = (EditText) findViewById(R.id.password);
mLogin = (Button) findViewById(R.id.login);
mRegistration = (Button) findViewById(R.id.registration);
mRegistration.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
String email = mEmail.getText().toString();
String password = mPassword.getText().toString();
mAuth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(CustomerLoginActivity.this, new OnCompleteListener<AuthResult>()
@Override
public void onComplete(@NonNull Task<AuthResult> task)
if(!task.isSuccessful())
Toast.makeText(CustomerLoginActivity.this, "sign up error", Toast.LENGTH_SHORT).show();
else
String user_id = mAuth.getCurrentUser().getUid();
DatabaseReference current_user_db = FirebaseDatabase.getInstance().getReference().child("Users").child("Customers").child(user_id);
current_user_db.setValue(true);
);
);
mLogin.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
String email = mEmail.getText().toString();
String password = mPassword.getText().toString();
mAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener(CustomerLoginActivity.this, new OnCompleteListener<AuthResult>()
@Override
public void onComplete(@NonNull Task<AuthResult> task)
if(!task.isSuccessful())
Toast.makeText(CustomerLoginActivity.this, "sign in error", Toast.LENGTH_SHORT).show();
);
);
@Override
protected void onStart()
super.onStart();
mAuth.addAuthStateListener(mAuthListener);
@Override
protected void onStop()
super.onStop();
mAuth.removeAuthStateListener(mAuthListener);
【问题讨论】:
什么是崩溃日志。显示崩溃日志!? 查看这个答案***.com/questions/39842599/… i.stack.imgur.com/a6ouh.png@Xenolion 检查@jignesh下面的答案,问题是您的编辑文本没有任何值,所以它们是空的,您可以在点击注册按钮之前填写它们来确认!! 【参考方案1】:需要验证邮箱,密码不能为空
String email = mEmail.getText().toString();
String password = mPassword.getText().toString();
if(password.length()==0 || email.length()==0)
// enter the email and password
return;
mAuth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(CustomerLoginActivity.this, new OnCompleteListener<AuthResult>()
@Override
public void onComplete(@NonNull Task<AuthResult> task)
if(!task.isSuccessful())
Toast.makeText(CustomerLoginActivity.this, "sign up error", Toast.LENGTH_SHORT).show();
else
String user_id = mAuth.getCurrentUser().getUid();
DatabaseReference current_user_db = FirebaseDatabase.getInstance().getReference().child("Users").child("Customers").child(user_id);
current_user_db.setValue(true);
);
【讨论】:
登录方法完美运行....它的注册方法使应用程序崩溃...您仍然建议我更改登录方法吗? @jignesh 非常感谢伙计!...非常感谢【参考方案2】:电子邮件和密码不应为空或为空
String email = mEmail.getText().toString();
String password = mPassword.getText().toString();
if(email.isEmpty() || password.isEmpty())
return; //you need to display message to the user
mAuth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(CustomerLoginActivity.this, new OnCompleteListener<AuthResult>()
...
)
【讨论】:
【参考方案3】:字符串电子邮件 = mEmail.getText().toString();
字符串密码 = mPassword.getText().toString();
/*请检查null,因为我们不能拥有它*/
if(email.isEmpty() || 密码.isEmpty()) 返回;
/*
你的代码
*/
【讨论】:
以上是关于按下注册按钮时应用程序崩溃的主要内容,如果未能解决你的问题,请参考以下文章