android - ButterKnife Bind in Fragment using different layouts -


i trying develop android application using fragments , stumbled on problem while trying use butterknife binds views , use @onclick annotation.

in fragment inflate different layouts depending on user's selection in menu. lets user selects settings inflate settings layout holds button logout. if user selects sync in menu inflate sync view holds button start sync.

my oncreateview looks following code:

public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = null; if(settings) {     rootview = inflater.inflate(r.layout.settings_view, container, false); } else {     rootview = inflater.inflate(r.layout.sync_view, container, false); } return rootview; 

}

so created @onclick method logout button @onclick(r.id.btnsettingslogout) public void logout() { toast.maketext(getactivity(), "button pressed!", toast.length_short).show(); } , added butterknife.bind(this, rootview); end of oncreateview method befure return rootview.

the problem when inflate settings view works , greeted toast message whenever press logout button when inflate sync view application crashes because of following exception:

java.lang.runtimeexception: unable bind views si.vitez.testapp.detailfragment 

is possible inject both views, application won't crash no mater of 2 views inflated?

you should use @nullable annotation method.

@nullabale @onclick(r.id.yourid) public void onclickmethod(){  // code } 

see here more information this.


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -