-2

public class forgetPassword extends AppCompatDialogFragment {

    public ResetDialogListener listener;
    EditText emailToReset;
    final String tag="finalProject.bhaa";
    String email =" ";

    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        LayoutInflater inflater = getActivity().getLayoutInflater();
        View view = inflater.inflate(R.layout.forget_password, null);
        builder.setView(view)
                .setTitle("Reset Password")
                .setNegativeButton("cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                    }
                })
                .setPositiveButton("Reset", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Log.i(tag,"---"+email);
                        email=emailToReset.getText().toString().trim();
                        Log.i(tag,"---"+email);
                        try {
                            listener.applyUpdate(email);
                        }catch (Exception e){
                            e.printStackTrace();
                            Log.i(tag,e.getMessage());
                        }
                    }
                });
        emailToReset= view.findViewById(R.id.emailToReset);
        return builder.create();
    }
    public interface ResetDialogListener {
        void applyUpdate(String Email);
    }
}

Here is the implemntation of applyUpdate:


    @Override
    public void applyUpdate(String Email) {
        Log.i(tag,"yyyyyyy");
        firebaseAuth.sendPasswordResetEmail(Email).addOnCompleteListener(new OnCompleteListener<Void>() {
            @Override
            public void onComplete(@NonNull Task<Void> task) {
                if (task.isSuccessful()){
                    Toast.makeText(MainActivity.this,"Check your email .. ",Toast.LENGTH_SHORT);
                }else {
                    String err=task.getException().getMessage();
                    Toast.makeText(MainActivity.this,"Error Occured: "+err,Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

Tis lin throw an exception : listener.applyUpdate(email);

Attempt to invoke interface method 'void com.bhaa.finalproject.forgetPassword$ResetDialogListener.applyUpdate(java.lang.String)' on a null object reference

and I need the help for this problem

Bhaa Rizik
  • 97
  • 1
  • 3
  • 10

1 Answers1

3

Make sure to initialize the listener when AppCompatDialogFragment attach.

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    listner = (ResetDialogListener) context
}
  • https://stackoverflow.com/questions/62902631/send-notification-from-device-to-another-with-broadcast-receiver-services-in-a – Bhaa Rizik Jul 17 '20 at 15:44