beginner
beginner

Reputation: 67

Fragment cannot be cast to java.util.concurrent.Executor error

I'm attempting to sign in or sign up using fragments, and after a successful sign-in/sign up, the HomeActivity supposed to load. The issue is that when I click the sign-in/sign up button, it returns to the same fragment instead of going to the HomeActivity.class. The app's initialization page is called LoginRegistrationActivity.class where it will call the SignIn fragment. I confirmed that Sign in/Sign Up is working well, but I am unable to navigate to the HomeActivity.class after successful authentication. I'm not sure if there is an issue with the AndroidManifest.xml file.

I got this logcat error after I clicked sign in:

java.lang.ClassCastException: com.fyp.selfzen.fragments.O_LoginRegistration.SignIn cannot be cast to java.util.concurrent.Executor at com.fyp.selfzen.fragments.O_LoginRegistration.SignIn.login(SignIn.java:124) at com.fyp.selfzen.fragments.O_LoginRegistration.SignIn$1.onClick(SignIn.java:87) at android.view.View.performClick(View.java:7448) at android.view.View.performClickInternal(View.java:7425) at android.view.View.access$3600(View.java:810) at android.view.View$PerformClick.run(View.java:28305) at android.os.Handler.handleCallback(Handler.java:938) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:223) at android.app.ActivityThread.main(ActivityThread.java:7656) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)

SignIn.java fragment

public class SignIn extends Fragment{
    private EditText editText_email, editText_password;
    private String email, password;
    private FirebaseAuth firebaseAuth;
    private ProgressDialog progressDialog;

LoginRegisrationActivity loginRegistration;

public SignIn(LoginRegisrationActivity loginRegistration) {
    this.loginRegistration = loginRegistration;
}

public static SignIn newInstance(LoginRegisrationActivity loginRegistration) {
    SignIn fragment = new SignIn(loginRegistration);
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_login, container, false);

    progressDialog = new ProgressDialog(getContext());
    firebaseAuth = FirebaseAuth.getInstance();
    editText_email = view.findViewById(R.id.editText_email_login_activity);
    editText_password = view.findViewById(R.id.editText_password_login_activity);
    TextView button_login = view.findViewById(R.id.button_login_activity);
    TextView textView_signup_login = view.findViewById(R.id.textView_signup_login);
    final SmoothCheckBox checkBox = view.findViewById(R.id.checkbox_login_activity);
    checkBox.setChecked(false);


    button_login.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            email = editText_email.getText().toString();
            password = editText_password.getText().toString();

            editText_email.clearFocus();
            editText_password.clearFocus();

            login(email, password);
        }
    });

    textView_signup_login.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            SignUp f2 = SignUp.newInstance(loginRegistration);
            loginRegistration.loadFrag(f2, getResources().getString(R.string.regis));
        }
    });
    return view;
}

public void login(String email, String password) {

    editText_email.setError(null);
    editText_password.setError(null);

    if (!isValidMail(email) || email.isEmpty()) {
        editText_email.requestFocus();
        editText_email.setError(getResources().getString(R.string.please_enter_email));
    }
    else if(password.isEmpty()){
        editText_password.requestFocus();
        editText_password.setError(getResources().getString(R.string.please_enter_password));
    }
    else {
        progressDialog.setMessage("Please wait...");
        progressDialog.show();
        progressDialog.setCanceledOnTouchOutside(false);

        firebaseAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener((Executor) this, new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {
                if (task.isSuccessful()) {
                    Toast.makeText(getContext(), "Successfully sign in!", Toast.LENGTH_LONG).show();
                  // Intent i = new Intent(loginRegistration, HomeActivity.class); //First try to go to HomeActivity
                    Intent i = new Intent(getContext(), HomeActivity.class); //Second try 
                    startActivity(i);
                    //loginRegistration.finish();
                } else {
                    Toast.makeText(getContext(), "Sign in failed", Toast.LENGTH_LONG).show();
                }
                progressDialog.dismiss();
            } //onComplete
        }); // firebaseAuth
    }// else
} // login end
}

SignUp.java fragments

public class SignUp extends Fragment{
    private EditText editText_name, editText_email, editText_password, editText_phoneNo;
    private String name, email, password, phoneNo;
    private ProgressDialog progressDialog;
    private FirebaseAuth firebaseAuth;

LoginRegisrationActivity loginRegistration;

public SignUp(LoginRegisrationActivity loginRegistration) {
    this.loginRegistration = loginRegistration;
    // Required empty public constructor
}

public static SignUp newInstance(LoginRegisrationActivity loginRegistration) {
    SignUp fragment = new SignUp(loginRegistration);
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_registration, container, false);

    progressDialog = new ProgressDialog(getContext());
    firebaseAuth = FirebaseAuth.getInstance();
    editText_name = view.findViewById(R.id.editText_name_register);
    editText_email = view.findViewById(R.id.editText_email_register);
    editText_password = view.findViewById(R.id.editText_password_register);
    editText_phoneNo = view.findViewById(R.id.editText_phoneNo_register);
    TextView button_submit = view.findViewById(R.id.button_submit);
    TextView textView_login = view.findViewById(R.id.textView_login_register);


    // Go to login page
    textView_login.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            SignIn f1 = SignIn.newInstance(loginRegistration);
            loginRegistration.loadFrag(f1, getResources().getString(R.string.login));
        }
    });


    button_submit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            name = editText_name.getText().toString();
            email = editText_email.getText().toString();
            password = editText_password.getText().toString();
            phoneNo = editText_phoneNo.getText().toString();

            form();
        }
    });
    return view;
}


private boolean isValidMail(String email) {
    return android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches();
}

public void form() {

    editText_name.setError(null);
    editText_email.setError(null);
    editText_password.setError(null);
    editText_phoneNo.setError(null);

    if (name.equals("") || name.isEmpty()) {
        editText_name.requestFocus();
        editText_name.setError(getResources().getString(R.string.please_enter_name));
    }
    else if (!isValidMail(email) || email.isEmpty()) {
        editText_email.requestFocus();
        editText_email.setError(getResources().getString(R.string.please_enter_email));
    }
    else if (password.equals("") || password.isEmpty()) {
        editText_password.requestFocus();
        editText_password.setError(getResources().getString(R.string.please_enter_password));
    }
    else if (phoneNo.equals("") || phoneNo.isEmpty()) {
        editText_phoneNo.requestFocus();
        editText_phoneNo.setError(getResources().getString(R.string.please_enter_phone));
    }
    else {
        editText_name.clearFocus();
        editText_email.clearFocus();
        editText_password.clearFocus();
        editText_phoneNo.clearFocus();
    }
    progressDialog.setMessage("Please wait...");
    progressDialog.show();
    progressDialog.setCanceledOnTouchOutside(false);
    firebaseAuth.createUserWithEmailAndPassword(email,password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
        @Override
        public void onComplete(@NonNull Task<AuthResult> task) {
            if(task.isSuccessful()){
                Toast.makeText(getContext(), "Successfully registered!", Toast.LENGTH_LONG).show();
                //Intent i = new Intent(loginRegistration, HomeActivity.class);
                Intent i = new Intent(getActivity(), HomeActivity.class);
                startActivity(i);
                //loginRegistration.finish();
            }
            else{
                Toast.makeText(getContext(), "Sign up failed", Toast.LENGTH_LONG).show();
            }
            progressDialog.dismiss();
        }
    });
} // end of form
}

LoginRegistrationActivity.class

public class LoginRegisrationActivity extends AppCompatActivity {
    private String curent;
    VideoView vide;

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

        vide = findViewById(R.id.vide);
        String video_url = "android.resource://" + getPackageName() + "/" + R.raw.login_video;
        Uri videoUri = Uri.parse(video_url);
        vide.setVideoURI(videoUri);
        vide.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                mp.setLooping(true);
                vide.requestFocus();
                vide.start();
            }
        });

        SignIn f1 = SignIn.newInstance(this);
        loadFrag(f1, getResources().getString(R.string.login));    
    }


    public void loadFrag(Fragment f1, String name) {

        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();

        if(!name.equals(curent)){
            curent =name;
            ft.replace(R.id.frame_layout, f1, name);
        }

        ft.commitAllowingStateLoss();

    }


}

AndroidManifest.xml

    <activity
        android:name="com.fyp.selfzen.activities.LoginRegisrationActivity"
        android:screenOrientation="portrait" />
    <activity
        android:name="com.fyp.selfzen.activities.HomeActivity"
        android:label="@string/title_activity_home"
        android:launchMode="singleTop"
        android:screenOrientation="portrait"
        android:windowSoftInputMode="adjustPan">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>

        <meta-data
            android:name="android.app.searchable"
            android:resource="@xml/searchable" />
    </activity>

    

Upvotes: 2

Views: 2219

Answers (2)

Chazz
Chazz

Reputation: 93

Have you solved your problem yet?

I ran into a similar but different issue when trying to use Fragments with FirebaseAuth. All of the documentation I found only referenced using Activities. Changing .addOnCompleteListener to .addOnSuccessListener and adding an .addOnFailureListener made my app work as expected. My app was originally crashing because of the Executor even though it would successfully authenticate users; the success/failure listeners fixed my issue.

My original code (written with help from Firebase Docs):

 private void authWithFirebase(String email, String password) {
    mAuth.signInWithEmailAndPassword(email, password)
            .addOnCompleteListener( (java.util.concurrent.Executor) this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    // If sign in fails, display a message to the user.
                       Log.w(TAG, "signInWithEmail:failure", task.getException());
                    if (task.isSuccessful()) {
                        // Sign in success, update UI with the signed-in user's information
                        Toast.makeText(getContext(), "Successfully signed in", Toast.LENGTH_SHORT).show();
                        Log.d(TAG, "signInWithEmail:success");
                        FirebaseUser user = mAuth.getCurrentUser();
                        FragmentFactory.startAdminFragment((MainActivity) getActivity());
                    } else
                        Toast.makeText( getContext(), "Authentication failed.", Toast.LENGTH_SHORT ).show();
                }
            });
}

Current/Working code:

private void authWithFirebase(String email, String password) {
    mAuth.signInWithEmailAndPassword(email, password)
            .addOnSuccessListener( new OnSuccessListener<AuthResult>() {
                @Override
                public void onSuccess(AuthResult authResult) {
                    FragmentFactory.startAdminFragment((MainActivity) getActivity());
                }
            })
            .addOnFailureListener( new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    // If sign in fails, display a message to the user.
                    Log.d(TAG, "signInWithEmail:failure");
                    Toast.makeText( getContext(), e.getMessage(), Toast.LENGTH_LONG ).show();
                }
            } );
}

Note:

I only have 1 Activity so I was able to login and continue to my desired Fragment but you should be able to modify my code (FragmentFactory.startAdminFragment((MainActivity) getActivity());) to continue to your HomeActivity.class

This video helped a lot: https://www.youtube.com/watch?v=fPhy1PKR1Wg I hope this helps.

Upvotes: 1

Vatsal Dholakiya
Vatsal Dholakiya

Reputation: 565

In SignIn.java and SignUp.java fragment you add addOnCompleteListener() for firebaseAuth. So, pass arguments in it,

Instead of,

firebaseAuth.createUserWithEmailAndPassword(email,password).addOnCompleteListener((Executor) this, new OnCompleteListener<AuthResult>() {}

To,

firebaseAuth.createUserWithEmailAndPassword(email,password).addOnCompleteListener(getContext(), new OnCompleteListener<AuthResult>() {}

IF, getContext() not work then try to write getActivity().

Upvotes: 0

Related Questions