简体   繁体   中英

Android, Butterknife, BindView, Final

There's probably a simple answer to this, but trying to use Butterknife to the fullest, converting some findViewById's into @BindViews, and noticed that I can't use BindView on views that I need to declare as final because Java raises an error that 'Variable 'x' might not have been initialized'.

Obviously, I don't need to use BindView, but just curious about this one to learn more about java. I read a bit about what the final declaration means, but couldn't tie it back to Butterknife. Thanks for the help.

It is not related to butterknife. If you want to initialize a member declared as final, you can either do it at the moment of the declaration or in the constructor. In the case of findViewById and, hence, Butterknife , those two options are off the table. In both cases you use findViewById which needs a valid context object and can be successfully used after setContentView , and those two conditions are never met, when it comes to initialize final members. Please, be aware that ButterKnife uses code generation and behind the scene it still uses findViewById as you would normally do

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