Butterknife的使用以及一些莫名其妙的错误解答
来源:互联网 发布:rip协议端口号 编辑:程序博客网 时间:2024/05/17 09:38
Butterknife 快速集成与使用步骤
注解中相对简单易懂的很不错的开源框架
1. 强大的View绑定和Click事件处理功能,简化代码,提升开发效率
2. 方便的处理Adaper里的ViewHolder绑定问题
3. 运行时不会影响app效率,使用配置方便
4. 代码清晰,可读性强
ButterKnife大大简化了程序员编写代码的速度,.并且在7.0版本以后引入了注解处理器,取代了之前利用反射原理进行findViewById影响APP性能的方式,不再影响APP运行效率,下面直接上图.
1、集成库;
在build.gradle里面添加这句:classpath 'com.jakewharton:butterknife-gradle-plugin:8.5.1'
2.在项目的build.gradle 添加 :
compile 'com.jakewharton:butterknife:8.5.1'annotationProcessor 'com.jakewharton:butterknife-compiler:8.5.1'
apply plugin: 'com.jakewharton.butterknife'示意图:
3、run一下:
可能会出现天杀的错误,不怕,赶紧百度一番,
Error:Execution failed for task ':app:transformClassesWithJarMergingForDebug'.> com.android.build.api.transform.TransformException: java.util.zip.ZipException: duplicate entry: android/support4/accessibilityservice/AccessibilityServiceInfoCompat$AccessibilityServiceInfoVersionImpl.class
前辈们给出解答:说是V4包重复了,于是你按照前辈的指引七搞八搞 ,
但是还没解决这个错误的话,
可能是这个原因:没有V4包
添加这一句试试看吧 :)
compile 'com.android.support:support-v4:25.1.1'
3 使用须知:
1.Activity中使用ButterKnife.bind(this);必须在setContentView();之后,父类中bind后,子类不需要再bind
2.Fragment中使用ButterKnife.bind(this, rootView); 根布局View对象,需要在onDestroyView中解绑
3.注解的属性不能用private或static修饰,否则会报错
4.Activity官方例子中没有解绑操作,而Fragment需要解绑
在Fragment中需要解绑
在fragment中使用(需要解绑)private Unbinder unbinder;@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_regist_create,container,false); unbinder = ButterKnife.bind(this, view);//需要加多一个加载了布局的View对象 return view;}@Overridepublic void onDestroyView() { super.onDestroyView(); unbinder.unbind();//fragment需要解绑
}
可以再偷懒一点
Android Studio插件:Android Butterknife Zelezny
方便给整个布局文件一键生成注解。
使用方法:
在setContentView(R.layout.activity_main)中对着activity_main右键,
generate ----> Generate ButterKnife Injections,勾选需要生成注解的控件即可
- Butterknife的使用以及一些莫名其妙的错误解答
- 一些莫名其妙的错误
- 编译时一些莫名其妙的错误
- Android 使用 EditText 出现莫名其妙的错误
- ASP 莫名其妙的错误
- 莫名其妙的错误窗口
- 【Android】莫名其妙的错误
- spark莫名其妙的错误
- 莫名其妙的错误
- C++ 程序编译的一些莫名其妙的错误。【2013.11.7】
- 一个莫名其妙的配置错误
- 一段莫名其妙错误的代码
- 莫名其妙的http 405错误
- 程序莫名其妙的错误,怎么办?
- 由莫名其妙的错误开始
- 遇到一个莫名其妙的错误。
- android studio使用ButterKnife 8.0.1报空指针错误以及点击事件无效的解决方法
- android studio使用ButterKnife 8.0.1报空指针错误以及点击事件无效的解决方法
- Linux学习之Yum本地源制作
- UESTC
- Linux下的文件描述符和文件指针的区别
- Android开发错误:Error:” ” is not translated in “en” (English) [MissingTranslation]如何处理?
- robots.txt 浅析
- Butterknife的使用以及一些莫名其妙的错误解答
- Hibernate5学习笔记(四)
- Codeforces Round #412 ( Div. 2) C
- Isolation Forest算法总结
- Go语言对数组进行冒泡排序和快速排序
- 配置ssm框架遇到的一个坑(<context:property-placeholder>占位符配置失效)
- Python数据采集之Requests
- springboot+security 动态权限修改session立即失效(六)
- IDEA中Tomcat相关——如无法访问localhost:8080欢迎页面,无法配置web.xml以及server.xml,设置download.xml下载目录无效等问题