android - How to use Firebase Email login with a fragment? -


my question title. here login code. when use this, got "cannot resolve method addoncompletelistener...." . android studio fix casting executor when casted got exception "fragment cannot cast executor" can't cast executor. how can solve problem ?

public class loginfragment extends fragment implements view.onclicklistener {  view v; button btnlogin; textview btnlostpw,btnregister; edittext txmail,txpass;   private firebaseauth mauth; private firebaseuser muser;  public loginfragment() {     // required empty public constructor }  @override public void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate); }  @override public view oncreateview(layoutinflater inflater, viewgroup container,                          bundle savedinstancestate) {     // inflate layout     v = inflater.inflate(r.layout.fragment_login, container, false);       btnlogin = (button) v.findviewbyid(r.id.login_btn_login);      btnlostpw =(textview) v.findviewbyid(r.id.login_btn_lost_pw);      btnregister=(textview) v.findviewbyid(r.id.login_btn_register);      txmail=(edittext)v.findviewbyid(r.id.login_email);      txpass=(edittext)v.findviewbyid(r.id.login_pass);      btnregister.setonclicklistener(this);     btnlostpw.setonclicklistener(this);     btnlogin.setonclicklistener(this);      mauth = firebaseauth.getinstance();     muser = firebaseauth.getinstance().getcurrentuser();      return v; }  @override public void onclick(view v) {        if(v.getid() == r.id.login_btn_lost_pw)     {         //password recovery     }      else if(v.getid() == r.id.login_btn_register)     {         //register     }     else if(v.getid() == r.id.login_btn_login)     {         loginuser(txmail.gettext().tostring(),txpass.gettext().tostring());     } }  private void loginuser(string mail, final string password) {      try {     mauth.signinwithemailandpassword(mail, password).addoncompletelistener(this, new oncompletelistener<authresult>() {         @override         public void oncomplete(@nonnull task<authresult> task) {             if(task.issuccessful())             {                 fragmenttransaction ft = getfragmentmanager().begintransaction();                 fragment mfrag = new nextfragment();                 ft.replace(r.id.fragment_container, mfrag);                 ft.commit();             }             else {                 snackbar.make(getactivity().findviewbyid(android.r.id.content),                         task.getexception().getlocalizedmessage(), snackbar.length_long).show();             }          }     });  } catch (exception ex) {     snackbar.make(getactivity().findviewbyid(android.r.id.content),             ex.getlocalizedmessage().tostring(), snackbar.length_long).show(); }  } 

}

edit : added codes in class.

thanks nivesh shastri help. here working code :

 mauth.signinwithemailandpassword(mail, password).addoncompletelistener((activity) getcontext(), new oncompletelistener<authresult>() {             @override             public void oncomplete(@nonnull task<authresult> task) {                 if(task.issuccessful())                 {                     fragmenttransaction ft = getfragmentmanager().begintransaction();                     fragment mfrag = new nextfragment();                     ft.replace(r.id.fragment_container, mfrag);                     ft.commit();                 }                 else {                     snackbar.make(getactivity().findviewbyid(android.r.id.content),                             task.getexception().getlocalizedmessage(), snackbar.length_long).show();                 }          }     }); 

try code:

private void loginuser(string mail, final string password) {  mauth.createuserwithemailandpassword(mail, password).addoncompletelistener(getcontext(), new oncompletelistener<authresult>() {  @override public void oncomplete(@nonnull task<authresult> task) {  if(task.issuccessful())  {  // task in success  } else {  // task in failure   } });   }  } 

Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -