Butterknife is the most popular open source library of the past two years and is a view injection framework whose main function is to replace the Findviewbyid () method. It is also one of the most useful tools to be able to quickly add listening events and make it easier to implement the MVVM pattern.
When you start using Butterknife, many people may encounter nullpointerexception or "can't find a symbol," because Butterknife is misconfigured. Let's take a detailed butterknife configuration
First we need to import Butterknife
can also be added here
Also need to install Butterknife plug-in
I've installed it here and it looks a little different. Search before you install, and then click the button below to get out.
Then configure
Note that if you do not complete the steps to install the plug-in above, APT () will not be recognized.
If you don't understand it, put the complete build.gradle down.
1 //Top-level build file where can add configuration options common to all sub-projects/modules.2 3 Buildscript {4 Repositories {5 Jcenter ()6 }7 Dependencies {8 classpath ' com.android.tools.build:gradle:2.1.0 '9 classpath ' com.neenbedankt.gradle.plugins:android-apt:1.8 'Ten //Note:do not place your application dependencies here; they belong One //In the individual module Build.gradle files A } - } - the allprojects { - Repositories { - Jcenter () - } + } - + Task Clean (type:delete) { A Delete Rootproject.builddir at}
1 Apply plugin: ' Com.android.application '2 3 Android {4 compilesdkversion5 buildtoolsversion "23.0.3"6 7 Defaultconfig {8 ApplicationID "Com.lcc.fjnu.damuvideobysohu"9 minsdkversionTen targetsdkversion One Versioncode 1 A Versionname "1.0" - } - Buildtypes { the Release { - minifyenabled false - proguardfiles getdefaultproguardfile (' proguard-android.txt '), ' Proguard-rules.pro ' - } + } - } + A Buildscript { at Repositories { - mavencentral () - } - Dependencies { - classpath ' com.neenbedankt.gradle.plugins:android-apt:1.8 ' - } in } - to Apply plugin: ' Com.neenbedankt.android-apt ' + - Dependencies { the compile Filetree (include: [' *.jar '], dir: ' Libs ') * testcompile ' junit:junit:4.12 ' $ compile ' com.android.support:appcompat-v7:23.3.0 'Panax Notoginseng compile ' com.android.support:design:23.3.0 ' - compile ' com.jakewharton:butterknife:8.1.0 ' the apt ' com.jakewharton:butterknife-compiler:8.1.0 ' +}
The above is the process of configuration
And again, a little bit of ButterKnife8.1.0. The binding method is @bindview before the version is @bind, before the @injectview.
Specific instructions can be referred to their official website (no wall) http://jakewharton.github.io/butterknife/
Here's a simple example:
1 Public classMainactivityextendsappcompatactivity {2 3 @BindView (R.id.toolbar)4 Toolbar Mtoolbar;5 @BindView (r.id.tabs)6 tablayout Mtabs;7 @BindView (R.id.appbar)8 appbarlayout Mappbar;9 @BindView (R.id.container)Ten Viewpager container; One @BindView (R.id.fab) A Floatingactionbutton Fab; -......}
No need to re-instantiate and Findviewbyid (), the code becomes more concise and clear
Android studio ButterKnife8.1.0 NULL pointer/related configuration and its simple use