我正在使用一个应用程序,最初有一个介绍屏幕,只有在第一次启动应用程序时才会运行。在最后一张幻灯片中,我有两个按钮,即signInAsCustomer和signInAsSeller。两个意图都会带您进行相同的活动,但会传递两个不同的意图附加内容。 signInAsCustomer传递额外的密钥"类型"和价值"客户"而signInAsSeler传递额外的密钥"类型"和价值"卖方" 。当第二个活动开始时,我得到额外的,并根据额外的,我在特定的文本视图中设置一些文本。 SignInActivity的代码如下。
SignInActivity
private String type;
private TextView tv_seller_or_customer , forgot_password , guest_login ;
private EditText email , password ;
private Button login , signUp;
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthStateListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sign_in);
// Initialising Firebase Auth Object
mAuth = FirebaseAuth.getInstance();
// This is the Auth state listener . It checks if the user is aldready signed In or not
mAuthStateListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user != null)
{
// User is signed in
// Goes to a new activity
Intent i = new Intent(SignInActivity.this , MainActivity.class);
startActivity(i);
}else
{
// User is signed out
// Shows sign in page to the user
signInUser();
}
}
};
}
@Override
protected void onResume() {
super.onResume();
mAuth.addAuthStateListener(mAuthStateListener);
}
@Override
protected void onPause() {
super.onPause();
if (mAuthStateListener != null)
{
mAuth.removeAuthStateListener(mAuthStateListener);
}
}
public void signInUser()
{
// This method initialises all the views in the signInPage
initialiseViews();
// Getting the intent extras and changing the activity depending upon the button pressed in the Intro Slider
Intent i = getIntent();
if (i != null) {
type = i.getStringExtra("type");
if (type.equals("seller")) {
tv_seller_or_customer.setText("Seller");
initialiseViews();
guest_login.setVisibility(View.GONE);
} else {
tv_seller_or_customer.setText("Customer");
initialiseViews();
}
}else
{
tv_seller_or_customer.setVisibility(View.GONE);
}
// Listener for loging in
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String emailString = email.getText().toString();
String passwordString = password.getText().toString().trim();
if (!Is_Valid_Email(emailString))
{
Toast.makeText(SignInActivity.this , "Please enter a valid Email ID", Toast.LENGTH_SHORT).show();
}else {
loginUser(emailString , passwordString);
}
}
});
}
public boolean Is_Valid_Email(String email_check) {
return !email_check.isEmpty() && isEmailValid(email_check);
}
boolean isEmailValid(CharSequence email) {
return android.util.Patterns.EMAIL_ADDRESS.matcher(email)
.matches();
}
public void loginUser(String emailLogin , String passwordLogin)
{
mAuth.signInWithEmailAndPassword(emailLogin , passwordLogin)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (!task.isSuccessful())
{
Log.w("sign in", "Failed" , task.getException());
}else
{
Intent i = new Intent(SignInActivity.this , MainActivity.class);
startActivity(i);
}
}
});
}
public void initialiseViews()
{
forgot_password = (TextView) findViewById(R.id.forgotPassword);
guest_login = (TextView) findViewById(R.id.guest_tv);
email = (EditText) findViewById(R.id.email_edt);
password = (EditText) findViewById(R.id.pass_edt);
login = (Button) findViewById(R.id.login_button);
signUp = (Button) findViewById(R.id.sign_up_btn);
}}
我得到的错误
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.ankit_pc.pharmahouse, PID: 15750
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at com.example.ankit_pc.pharmahouse.SignInActivity.signInUser(SignInActivity.java:78)
at com.example.ankit_pc.pharmahouse.SignInActivity$1.onAuthStateChanged(SignInActivity.java:48)
at com.google.firebase.auth.FirebaseAuth$1.run(Unknown Source)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7325)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) Application terminated.
我哪里出错?
答案 0 :(得分:1)
您似乎忘记了初始化tv_seller_or_customer
。只需将其添加到您的initialiseViews()
函数中,您就应该好了。
e.g:
tv_seller_or_customer = (TextView) findViewById(R.id.tv_seller_or_customer)
答案 1 :(得分:1)
在initialiseViews()
中,您没有初始化tv_seller_or_customer
,因此它会抛出NullPointerException。
Intialise tv_seller_or_customer
,它会正常工作。
答案 2 :(得分:0)
在oncreate of activity中调用initialiseViews()。不需要调用其他方法只需在活动中的oncreate方法中调用一次。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sign_in);
initialiseViews();
///other code
}