简体   繁体   中英

How can I access a setter method of MainActivity class, from a fragment classe?

This is the setter method in MainActivity class

public void setNumPlats(int numero){
this.numPlats = numero;
}

I'm trying to acces to this method from fragment class, but i dont know how i need to do this.

In the fragment class i'm trying to do this

I'm trying it, and it doesnt makes error when i put it into onAttach, but when i execute the app it crashes. @Override
public void onAttach(Activity activity) {
    super.onAttach(activity);

}

((MainActivity)getActivity()).setNumPlats(adapter.getNumPlats());
@Override
public void onDetach() {
    super.onDetach();
}

从片段中尝试以下操作: ((MainActivity)getActivity()).setNumPlats(20)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM