嗨,所以我正在使用firebase进行登录/注册系统,出于某种原因
startActivity
只是不想工作。这里是
主要活动(登录屏幕)
package com.example.noam.photoupload;
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.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.analytics.FirebaseAnalytics;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
public class LoginScreen extends AppCompatActivity implements
View.OnClickListener {
private ProgressDialog Loading;
private FirebaseAuth mFireAuth;
private Button Registar;
private EditText Email;
private EditText Passowrd;
private Button Signin;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login_screen);
Loading= new ProgressDialog(this);
Registar=(Button) findViewById(R.id.Register);
Email=(EditText)findViewById(R.id.EditEmail);
Passowrd=(EditText)findViewById(R.id.EditPassword);
Signin=(Button)findViewById(R.id.SignInlink);
Registar.setOnClickListener(this);
Signin.setOnClickListener(this);
mFireAuth= FirebaseAuth.getInstance();
if(mFireAuth.getCurrentUser() !=null){
finish();
startActivity(new Intent(getApplicationContext(),ProfileActive.class));
}
}
private void RegistarUser(){
String email=Email.getText().toString().trim();
String password=Passowrd.getText().toString().trim();
if(TextUtils.isEmpty(email)){
//email is empty
Toast.makeText(this,"Please enter email",Toast.LENGTH_SHORT).show();
return;
}
if(TextUtils.isEmpty(password)){
//Passowrd is empty
Toast.makeText(this,"Please enter Password",Toast.LENGTH_SHORT).show();
return;
}
Loading.setMessage("Reguistering user...");
Loading.show();
mFireAuth.createUserWithEmailAndPassword(email,password)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
Loading.dismiss();
if(task.isSuccessful()){
//user is Registerd ,and login
//add my start to the next screen
finish();
startActivity(new Intent(getApplicationContext(),ProfileActive.class));
}
else{
Toast.makeText(LoginScreen.this,"failed",Toast.LENGTH_SHORT).show();
}
}
});
}
@Override
public void onClick(View view) {
if(view ==Registar){
RegistarUser();
}
if(view == Signin){
//LoginInActivity
finish();
Intent i = new Intent(LoginScreen.this, SignIn.class);
startActivity(i);
}
}
主类XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
<Button
android:layout_margin="15dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/Register"
android:text="Register User"/>
<Button
android:layout_margin="15dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Already Registered? Sign in here"
android:id="@+id/SignInlink"
android:gravity="center"/>
</LinearLayout>
</RelativeLayout>
这是第二类(重要的是,由于某种原因,它也给了我一个错误:
“java.lang.NullPointerException:尝试调用虚方法 “无效 android.widget.Button.setOnClickListener(android.view.View $ OnClickListener)'
对空对象引用“)
package com.example.noam.photoupload;
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.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.analytics.FirebaseAnalytics;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
public class SignIn extends AppCompatActivity implements
View.OnClickListener {
private ProgressDialog Loading;
private FirebaseAuth mFireAuth;
private Button Login;
private EditText Email;
private EditText Passoword;
private TextView SignUp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);Loading= new ProgressDialog(this);
setContentView(R.layout.activity_login_screen);
Login=(Button) findViewById(R.id.login);
Email=(EditText)findViewById(R.id.EditEmail);
Passoword=(EditText)findViewById(R.id.EditPassword);
SignUp=(TextView)findViewById(R.id.Signuplink);
mFireAuth= FirebaseAuth.getInstance();
Login.setOnClickListener(this);
SignUp.setOnClickListener(this);
;
}
private void UserLogin(){
String email=Email.getText().toString().trim();
String password=Passoword.getText().toString().trim();
if(TextUtils.isEmpty(email)){
//email is empty
Toast.makeText(this,"Please enter email",Toast.LENGTH_SHORT).show();
return;
}
if(TextUtils.isEmpty(password)){
//Passowrd is empty
Toast.makeText(this,"Please enter Password",Toast.LENGTH_SHORT).show();
return;
}
Loading.setMessage("Reguistering user...");
Loading.show();
mFireAuth.signInWithEmailAndPassword(email,password)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
Loading.dismiss();
if(task.isSuccessful()){
//user is Registerd ,and login
//add my start to the next screen
finish();
startActivity(new Intent(getApplicationContext(),ProfileActive.class));
}
else{
Toast.makeText(SignIn.this,"failed",Toast.LENGTH_SHORT).show();
}
}
});
}
@Override
public void onClick(View view) {
if(view ==Login){
UserLogin();
}
if(view == SignUp){
//LoginInActivity
finish();
startActivity(new Intent(this,LoginScreen.class));
}
}
第二类XML
<Button
android:layout_margin="15dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/login"
android:text="Login"/>
<TextView
android:layout_margin="15dp"
android:onClick="onClick"
android:clickable="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Don't have an account yet ? Signup here "
android:id="@+id/Signuplink"
android:gravity="center"/>
(在尝试建议的解决方案后): 更新1: 我“删除”了SignIn代码以检查问题是否在SignIn内,并且主类(LoginScreen)仍然没有启动下一个活动,并且由于某种原因它还给了我“输入电子邮件”Toast,这意味着它输入了另一个如果由于某种原因(?)但这次应用程序没有崩溃!
更新2: 我想找到调用RegisterUser()并修复它的bug,但是再次,同样的bug每当我在Loginscreen中单击“SignInLink”时,如果在SignIn类中有相同的代码,它会崩溃应用程序,所以我再次决定“删除”SignIn类中的所有代码(只保留带有xml视图的类),现在当我按下SignInLink时,它只是再次调用mainclass而不起作用(也许它调用登录屏幕但不会改变按钮的视图?我真的不知道!)
答案 0 :(得分:2)
更改所有类似的代码
finish();
startActivity(new Intent(getApplicationContext(),ProfileActive.class));
以强>
startActivity(new Intent(getApplicationContext(),ProfileActive.class));
finish();
并在LoginScreen
班级
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.Register:
RegistarUser();
break;
case R.id.SignInlink:
Intent i = new Intent(LoginScreen.this, SignIn.class);
startActivity(i);
finish();
break;
}
}
并更改SignIn
课程
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.login:
UserLogin();
break;
case R.id.Signuplink:
startActivity(new Intent(this,LoginScreen.class));
finish();
break;
}
}
修改强>
<TextView
android:layout_margin="15dp"
android:clickable="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Don't have an account yet ? Signup here "
android:id="@+id/Signuplink"
android:gravity="center"/>
只需删除代码中的android:onClick="onClick"
即可。
我尝试这样的代码就可以了。你可以尝试一下。
<强> EDIT2 强>
<强> LoginScreen 强>
public class LoginScreen extends AppCompatActivity implements
View.OnClickListener {
private ProgressDialog Loading;
// private FirebaseAuth mFireAuth;
private Button Registar;
private EditText Email;
private EditText Passowrd;
private Button Signin;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login_screen);
Loading = new ProgressDialog(this);
Registar = (Button) findViewById(R.id.Register);
// Email=(EditText)findViewById(R.id.EditEmail);
// Passowrd=(EditText)findViewById(R.id.EditPassword);
Signin = (Button) findViewById(R.id.SignInlink);
Registar.setOnClickListener(this);
Signin.setOnClickListener(this);
}
private void RegistarUser() {
// String email = Email.getText().toString().trim();
// String password = Passowrd.getText().toString().trim();
if (TextUtils.isEmpty("email")) {
//email is empty
Toast.makeText(this, "Please enter email", Toast.LENGTH_SHORT).show();
return;
}
if (TextUtils.isEmpty("password")) {
//Passowrd is empty
Toast.makeText(this, "Please enter Password", Toast.LENGTH_SHORT).show();
return;
}
Loading.setMessage("Reguistering user...");
Loading.show();
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.Register:
RegistarUser();
break;
case R.id.SignInlink:
Intent i = new Intent(LoginScreen.this, SignIn.class);
startActivity(i);
finish();
break;
}
}
}
<强>登入强>
public class SignIn extends AppCompatActivity implements
View.OnClickListener {
private ProgressDialog Loading;
private Button Login;
private EditText Email;
private EditText Passoword;
private TextView SignUp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Loading = new ProgressDialog(this);
setContentView(R.layout.activity_sign_in);
Login = (Button) findViewById(R.id.login);
// Email=(EditText)findViewById(R.id.EditEmail);
// Passoword=(EditText)findViewById(R.id.EditPassword);
SignUp = (TextView) findViewById(R.id.Signuplink);
Login.setOnClickListener(this);
SignUp.setOnClickListener(this);
;
}
private void UserLogin() {
// String email = Email.getText().toString().trim();
// String password = Passoword.getText().toString().trim();
if (TextUtils.isEmpty("email")) {
//email is empty
Toast.makeText(this, "Please enter email", Toast.LENGTH_SHORT).show();
return;
}
if (TextUtils.isEmpty("password")) {
//Passowrd is empty
Toast.makeText(this, "Please enter Password", Toast.LENGTH_SHORT).show();
return;
}
Loading.setMessage("Reguistering user...");
Loading.show();
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.login:
UserLogin();
break;
case R.id.Signuplink:
startActivity(new Intent(this, LoginScreen.class));
finish();
break;
}
}
}