Butterknife: Toolbar Binding is not working

Created on 2 May 2016  路  5Comments  路  Source: JakeWharton/butterknife

public class MainActivity extends AppCompatActivity {

@BindView(R.id.activityMainToolbar)
Toolbar mToolbar;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ButterKnife.bind(MainActivity.this);
    setSupportActionBar(mToolbar);
}

}

This always returns my mToolbar null!!!!!
What's wrong?

Needs Info

Most helpful comment

You need to include butterknife-compiler in your build.gradle per the README otherwise the generated code will not be present.

All 5 comments

What version? Did you include the compiler? Is the generated code present? What do the logs say when you enable them?

compile 'com.jakewharton:butterknife:8.0.1'

When I try to change the toolbar title, for example, using "mToolbar.setTitle("title");", it doesn't work.
Look:

"[...]Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.Toolbar.setTitle(java.lang.CharSequence)' on a null object reference[...]"

When I use "mToolbar = (Toolbar) findViewById(R.id.activityMainToolbar);", and set a new title it works fine. :/

You need to include butterknife-compiler in your build.gradle per the README otherwise the generated code will not be present.

Ok, then! I'll try it!

Hell yea! It works fine. I didn't read the new readme :(
Thanks!

Was this page helpful?
0 / 5 - 0 ratings