应用程序在打开新活动时崩溃

时间:2017-06-09 19:13:51

标签: android firebase firebase-realtime-database firebase-authentication

我是Android编程的新手,但想要创建一个可以存储用户的应用程序' Firebase上的数据并将其恢复。我将身份验证工作,但是一旦我将其重定向到他们必须填写详细信息的表单,但每当我尝试登录时它都会崩溃。

package example.com.myapplication;


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.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;




public class MainActivity extends AppCompatActivity {



private Button Signup, Signin, FPbtn;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final FirebaseAuth mAuth;

    mAuth = FirebaseAuth.getInstance();


    Signin = (Button) findViewById(R.id.Loginbtn);
    Signup = (Button) findViewById(R.id.signupbtn);
    FPbtn = (Button) findViewById(R.id.FPBtn);

    //Forgot Password

    FPbtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(new Intent(MainActivity.this, ResetPassword.class));
        }
    });

    /* //Register Method

    Signup.setOnClickListener(new View.OnClickListener() {


        EditText email = (EditText) findViewById(R.id.usernamefield);
        EditText password = (EditText) findViewById(R.id.passwordfield);
        String email3 = email.getText().toString();
        final String password3 = password.getText().toString();

        @Override
        public void onClick(View v) {
            if (TextUtils.isEmpty(email3)) {
                Toast.makeText(getApplicationContext(), "Enter email address!", Toast.LENGTH_SHORT).show();
                return;
            }

            if (TextUtils.isEmpty(password3)) {
                Toast.makeText(getApplicationContext(), "Enter password!", Toast.LENGTH_SHORT).show();
            } else
                {
                startActivity(new Intent(MainActivity.this, OTP.class));
            }


        }
    }); */

    Signup.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(new Intent(MainActivity.this, sign_up.class));
        }
    });


    //Login Method

    Signin.setOnClickListener(new View.OnClickListener()


    {
        @Override
        public void onClick(View v) {
            EditText email = (EditText) findViewById(R.id.usernamefield);
            EditText password = (EditText) findViewById(R.id.passwordfield);
            String email2 = email.getText().toString();
            final String password2 = password.getText().toString();


            if (TextUtils.isEmpty(email2)) {
                Toast.makeText(getApplicationContext(), "Enter email address!", Toast.LENGTH_SHORT).show();
                return;
            }

            if (TextUtils.isEmpty(password2)) {
                Toast.makeText(getApplicationContext(), "Enter password!", Toast.LENGTH_SHORT).show();
                return;
            }

            mAuth.signInWithEmailAndPassword(email2, password2)
                    .addOnCompleteListener(MainActivity.this, new OnCompleteListener<AuthResult>() {
                        @Override
                        public void onComplete(@NonNull Task<AuthResult> task) {
                            if (!task.isSuccessful()) {
                                Toast.makeText(MainActivity.this, getString(R.string.auth_failed), Toast.LENGTH_LONG).show();
                            } else {
                                Intent intent = new Intent(MainActivity.this, Profile.class);
                                startActivity(intent);
                                finish();
                            }
                        }
                    });

        }

    });
    }
}

然后这是我要将其重定向到

的Profile.Java文件
package example.com.myapplication;

import android.content.Intent;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;


import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;




public class Profile extends AppCompatActivity {

private EditText t1,t2,t3,t4,t5;
private Button b1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_profile);
    b1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            t1 = (EditText)findViewById(R.id.FirstNameField);
            t2 = (EditText)findViewById(R.id.LastNameField);
            t3 = (EditText)findViewById(R.id.AgeField);
            t4 = (EditText)findViewById(R.id.WL1);
            t5 = (EditText)findViewById(R.id.WL2);
            b1 = (Button)findViewById(R.id.ProfSubmit);

            FirebaseDatabase User1 = FirebaseDatabase.getInstance();
            DatabaseReference Name = User1.getReference("Fname");
            DatabaseReference LName = User1.getReference("LName");
            DatabaseReference age = User1.getReference("age");
            DatabaseReference Work1 = User1.getReference("WorkLink1");
            DatabaseReference Work2 = User1.getReference("WorkLink2");

            Name.setValue(t1.getText());
            LName.setValue(t2.getText());
            age.setValue(t3.getText());
            Work1.setValue(t4.getText());
            Work2.setValue(t5.getText());

            startActivity(new Intent(Profile.this, HomeScreen.class));
            finish();
        }
    });



// ...
    }
}

1 个答案:

答案 0 :(得分:0)

将此代码替换为第一个文件,并为此代码中定义的相应按钮添加onClick方法

public class MainActivity extends AppCompatActivity {

private Button Signup, Signin, FPbtn;    
EditText email,password;
final FirebaseAuth mAuth;    
Context cntx;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mAuth = FirebaseAuth.getInstance();

    email = (EditText) findViewById(R.id.usernamefield);
    password = (EditText) findViewById(R.id.passwordfield);

    Signin = (Button) findViewById(R.id.Loginbtn);
    Signup = (Button) findViewById(R.id.signupbtn);
    FPbtn = (Button) findViewById(R.id.FPBtn);                        
    cntx = this;
}        

//Declare this method as onClick method for Fpbtn 
public void FpbtnClickMethod(View view) {
    startActivity(new Intent(cntx, ResetPassword.class));
}

//Declare this method as onClick method for SignUp button
public void SignupOnClickMethod(View view){
    startActivity(new Intent(cntx, sign_up.class));
}        

//Declare this method as onClick method for Sign In button
public void SignInOnClickMethod(View view){
    String email2 = email.getText().toString();
    final String password2 = password.getText().toString();


    if (TextUtils.isEmpty(email2)) {
        Toast.makeText(getApplicationContext(), "Enter email address!", Toast.LENGTH_SHORT).show();
        return;
    }

    if (TextUtils.isEmpty(password2)) {
        Toast.makeText(getApplicationContext(), "Enter password!", Toast.LENGTH_SHORT).show();
        return;
    }

    mAuth.signInWithEmailAndPassword(email2, password2)
            .addOnCompleteListener(cntx, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    if (!task.isSuccessful()) {
                        Toast.makeText(cntx, getString(R.string.auth_failed), Toast.LENGTH_LONG).show();
                    } else {
                        Intent intent = new Intent(cntx, Profile.class);
                        startActivity(intent);
                        finish();
                    }
                }
            });
}
}

这里,cntx变量将解决onClick的匿名内部类中出现的上下文问题