android - setOnCheckedChangeListener(this) error -


i'm beginner in android. read books. create checkbox

import android.os.bundle; import android.support.design.widget.floatingactionbutton; import android.support.design.widget.snackbar; import android.support.v7.app.appcompatactivity; import android.support.v7.widget.toolbar; import android.view.view; import android.view.menu; import android.view.menuitem; import android.widget.checkbox; import android.widget.compoundbutton; import android.widget.compoundbutton.oncheckedchangelistener;  public class mainactivity extends appcompatactivity implements oncheckedchangelistener { checkbox cb;  @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);     toolbar toolbar = (toolbar) findviewbyid(r.id.toolbar);     setsupportactionbar(toolbar);      floatingactionbutton fab = (floatingactionbutton) findviewbyid(r.id.fab);     fab.setonclicklistener(new view.onclicklistener() {         @override         public void onclick(view view) {             snackbar.make(view, "replace own action", snackbar.length_long)                     .setaction("action", null).show();              cb = (checkbox) findviewbyid(r.id.mycheckbox);             cb.setoncheckedchangelistener(this);         }     }); }  @override public boolean oncreateoptionsmenu(menu menu) {     // inflate menu; adds items action bar if present.     getmenuinflater().inflate(r.menu.menu_main, menu);     return true; }  @override public boolean onoptionsitemselected(menuitem item) {     // handle action bar item clicks here. action bar     // automatically handle clicks on home/up button, long     // specify parent activity in androidmanifest.xml.     int id = item.getitemid();      //noinspection simplifiableifstatement     if (id == r.id.action_settings) {         return true;     }      return super.onoptionsitemselected(item); }  @override public void oncheckedchanged(compoundbutton buttonview, boolean ischecked) {     if (ischecked) {         cb.settext("checked");     } else {         cb.settext("uncheck");     } } 

}

when use cb.setoncheckedchangelistener(this); android studio error: setoncheckedchangelistener(android.widget.compoundbutton.oncheckedchangelistener) in compoundbutton cannot applied (anonymous android.view.view.onclicklistener)

if change code such as:

cb.setoncheckedchangelistener(new oncheckedchangelistener() {      @override     public void oncheckedchanged(compoundbutton buttonview, boolean ischecked) {         //do stuff      } });     

then ok

can me. want use "this".

use mainactivity.this instead of this :

 cb.setoncheckedchangelistener(mainactivity.this); 

because in current code this refer button onclick method context instead of mainactivity class in oncheckedchangelistener interface in implemented.


Comments

Popular posts from this blog

c++ - llvm function pass ReplaceInstWithInst malloc -

java.lang.NoClassDefFoundError When Creating New Android Project -

Decoding a Python 2 `tempfile` with python-future -