-5

I can't move to the LoginPageActivity.I think the problem is in my 'Intent' statement. Maybe, I gave wrong 'context'. Everything looks good. But application is crushing. I couldn't find the problem. Can you help me? What shall I give as a context in the MainActivity and ELSE statement?

I think the problem is Intent in Helper class.

I have an IF condition like this:

public void createNewUser(final Context context, String email, String password){
        firebaseAuth = FirebaseAuth.getInstance();
        firebaseAuth.createUserWithEmailAndPassword(email, password)
            .addOnCompleteListener((Activity) context, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    if (!task.isSuccessful()) {
                        ShortCut.displayMessageToast(context,  "invalid info");
                    }else {
                        ShortCut.displayMessageToast(context, "Account created");
                        Intent loginIntent = new Intent(context, LoginPageActivity.class);
                        context.startActivity(loginIntent);
                    }
                }
            });
}

When I am trying to create a new account my else condition is works. But, why can't I move to LoginPageActivity?

And, I call from here in MainActivity:

@OnClick(R.id.createBtn)
void createButonClicked(){

    String email = mEmail.getText().toString();
    String password = mPassword.getText().toString();
    String clientName = mClientName.getText().toString();
    String clientSurname = mClientSurname.getText().toString();
    String clientCity = mClientCity.getText().toString();
    String clientPhone = mClientPhone.getText().toString();
    if (TextUtils.isEmpty(email)
            || TextUtils.isEmpty(password)
            || TextUtils.isEmpty(clientName)
            || TextUtils.isEmpty(clientSurname)
            || TextUtils.isEmpty(clientCity)
            || TextUtils.isEmpty(clientPhone))
    {
        ShortCut.displayMessageToast(this, "You should fill empty fields!");

    }else {

        firebaseApplication = new FirebaseApplication();
        firebaseApplication.createNewUser(this, email, password);
        ShortCut.displayMessageToast(this, "just for a debug");
    }
}
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
K.tas
  • 214
  • 1
  • 3
  • 16
  • 1
    Look at [the stack trace](https://stackoverflow.com/questions/23353173) to determine the cause of the crash. – Mike M. Jan 12 '19 at 19:42
  • 2
    Possible duplicate of [Unfortunately MyApp has stopped. How can I solve this?](https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this) – Vladyslav Matviienko Jan 12 '19 at 20:37

2 Answers2

0

When you call the method "createNewUser()", you are inside of an onClickListener. When you pass in 'this' as your context, you are passing in the context of your listener. Instead, when calling createNewUser, use 'MainActivity.this' as your Context, so then your app knows that the context is the whole activity and not just the listener. That means the code should be

}else {

    firebaseApplication = new FirebaseApplication();
    firebaseApplication.createNewUser(MainActivity.this, email, password);
    ShortCut.displayMessageToast(this, "just for a debug");
}
0

I found my fault(s):

Firstly, I need a code in MainActivity's ELSE statement like this:

((FirebaseApplication)getApplication()).createNewUser(MainActivity.this, email, password);

Secondly, I need change the content of AndroidManifest.xml LoginPageActivity must be declared in:

<application>
...
...-->Another activity...
...
   <activity android:name=".LoginPageActivity">
       <intent-filter>
           <action android:name="android.intent.action.MAIN" />

           <category android:name="android.intent.category.LAUNCHER" />
       </intent-filter>
   </activity>
</application>
K.tas
  • 214
  • 1
  • 3
  • 16