我是Android新手。我从互联网上复制了这段代码。此代码以前运行过。但现在应用程序崩溃了。 this shows up after clicking register不知道它崩溃的原因。我在Android Studio上使用Firebase处理登录注册类应用。它构建得很好,但是当我在手机上运行它时,启动器活动可以正常工作,但是当我使用我的按钮访问我的Firebase登录活动时会出现问题。我如何解决崩溃?
Java文件。
package com.example.omkar.smileservices;
import android.app.ProgressDialog;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Patterns;
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 signup extends AppCompatActivity implements View.OnClickListener{
private EditText inputName,inputEmail, inputPassword, inputConfirmPassword;
private Button btnRegister, btnSignIn;
private ProgressDialog progressDialog;
private FirebaseAuth mAuth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_signup);
mAuth = FirebaseAuth.getInstance();
if(mAuth.getCurrentUser() != null){
//profile activity
finish();
startActivity(new Intent(signup.this,home.class));
}
progressDialog =new ProgressDialog(this);
btnRegister = (Button) findViewById(R.id.register);
btnSignIn = (Button) findViewById(R.id.signin);
inputName = (EditText)findViewById(R.id.name);
inputEmail = (EditText) findViewById(R.id.email);
inputPassword = (EditText) findViewById(R.id.password);
inputConfirmPassword = (EditText)findViewById(R.id.conpassw);
btnRegister.setOnClickListener(this);
btnSignIn.setOnClickListener(this);
}
@Override
public void onStart() {
super.onStart();
// Check if user is signed in (non-null) and update UI accordingly.
FirebaseUser currentUser = mAuth.getCurrentUser();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.signin:
startActivity(new Intent(signup.this,login.class));
break;
case R.id.register:
register();
break;
}
}
public void register(){
String name = inputName.getText().toString().trim();
String email = inputEmail.getText().toString().trim();
String password = inputPassword.getText().toString().trim();
String confirmpassword = inputConfirmPassword.getText().toString().trim();
if (TextUtils.isEmpty(name)) {
Toast.makeText(getApplicationContext(), "Enter email address!", Toast.LENGTH_SHORT).show();
return;
}
if (TextUtils.isEmpty(email)) {
Toast.makeText(getApplicationContext(), "Enter email address!", Toast.LENGTH_SHORT).show();
return;
}
if (!Patterns.EMAIL_ADDRESS.matcher(email).matches()){
Toast.makeText(getApplicationContext(), "Enter valid email address!", Toast.LENGTH_SHORT).show();
return;
}
if (TextUtils.isEmpty(password)) {
Toast.makeText(getApplicationContext(), "Enter password!", Toast.LENGTH_SHORT).show();
return;
}
if (TextUtils.isEmpty(confirmpassword)) {
Toast.makeText(getApplicationContext(), "Enter password!", Toast.LENGTH_SHORT).show();
return;
}
if (password.length() < 6) {
Toast.makeText(getApplicationContext(), "Password too short, enter minimum 6 characters!", Toast.LENGTH_SHORT).show();
return;
}
if(!confirmpassword.matches(password)){
inputConfirmPassword.setError("Doesn't match the Password.");
inputConfirmPassword.requestFocus();
return;
}
progressDialog.setMessage("Registering..");
progressDialog.show();
//create user
mAuth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(signup.this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
Toast.makeText(signup.this, "createUserWithEmail:onComplete:" + task.isSuccessful(), Toast.LENGTH_SHORT).show();
if (!task.isSuccessful()) {
// progressDialog.dismiss();
Toast.makeText(signup.this, "Unable to Register. Please try Later", Toast.LENGTH_LONG).show();
} else {
// progressDialog.dismiss();
Toast.makeText(signup.this, "Registered Successfully..", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(signup.this, home.class);
finish();
startActivity(intent);
}
}
});
}
}
崩溃日志
03-28 13:19:30.498 24522-24540/com.example.omkar.smileservices D/FA: Logging
event (FE): screen_view(_vs), Bundle[{firebase_event_origin(_o)=auto,
firebase_previous_class(_pc)=login,
firebase_previous_id(_pi)=9003268579764646367,
firebase_screen_class(_sc)=signup,
firebase_screen_id(_si)=9003268579764646368}]
03-28 13:19:54.733 24522-24522/com.example.omkar.smileservices
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.omkar.smileservices, PID: 24522
java.lang.NullPointerException: Attempt to invoke virtual method
'android.text.Editable android.widget.EditText.getText()' on a null object reference
at com.example.omkar.smileservices.signup.register(signup.java:75)
at com.example.omkar.smileservices.signup.onClick(signup.java:68)
at android.view.View.performClick(View.java:5637)
at android.view.View$PerformClick.run(View.java:22429)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
答案 0 :(得分:0)
崩溃是由NullPointerException引起的,很可能是由以下行引起的:
inputName.getText().toString().trim();
正如日志中的错误所示:
尝试在null上调用虚方法
EditText.getText()
对象参考
很明显,空对象引用是inputName
,它应该指向XML布局中的editText。
因此,在以下一行
inputName = (EditText) findViewById(R.id.name);
findViewById
方法未成功将视图绑定到名为inputName
的EditText(view)变量。
正如评论中所建议的那样,您可以通过检查已设置的XML布局文件(即activity_signup.xml)是否包含ID为name
的EditText来解决此问题。