美文网首页
Butterknife配置及常见错误

Butterknife配置及常见错误

作者: 隗豪 | 来源:发表于2020-05-27 22:15 被阅读0次

    一、Android Studio配置Butterknife

    实验环境:Android Studio 3.5.2 版本、SDK API 29、Butterknife 1.6.0

    1.打开Android Studio的Settings进行配置

    2.找到Settings中的Plugins,在搜索框中输入Butterknife

    插件:Android ButterKnife Zelezny
    点击 install 然后 Restart IDE



    3.进行build.gradle(Module:app)配置

    android {  
      ...
      compileOptions {  
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
      } 
    
    dependencies {
        implementation 'com.jakewharton:butterknife:10.1.0'
        annotationProcessor 'com.jakewharton:butterknife-compiler:10.1.0'
    }
    

    二、Butterknife的使用

    1.XML创建组件

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <Button
            android:text="测试案例_1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/Btn_lab_1">
        </Button>
        <Button
            android:text="测试案例_2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/Btn_lab_2">
        </Button>
    </LinearLayout>
    

    2.在Android Studio 选中MainActivity中布局文件名称点击右键,然后点击Generate


    3.选择是否生成触发事件及修改变量名称

    4.完整代码

    • Activity
    public class MainActivity extends AppCompatActivity {
        @BindView(R.id.Btn_lab_1)
        Button BtnLab1;
        @BindView(R.id.Btn_lab_2)
        Button BtnLab2;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            ButterKnife.bind(this);
        }
    
    
        @OnClick({R.id.Btn_lab_1, R.id.Btn_lab_2})
        public void onViewClicked(View view) {
            switch (view.getId()) {
                case R.id.Btn_lab_1:
                    Toast.makeText(getApplicationContext(),"Btn_lab_1",Toast.LENGTH_LONG).show();
                    break;
                case R.id.Btn_lab_2:
                    Toast.makeText(getApplicationContext(),"Btn_lab_2",Toast.LENGTH_LONG).show();
                    break;
            }
        }
    }
    
    
    • Fragment
    public class ThisFragment extends Fragment {
        private Context context;
        private static ThisFragment thisFragment;
    
        private ThisFragment(Context context) {
            this.context = context;
        }
    
        public static ThisFragment getThisFragment(Context context) {
            if (thisFragment == null) {
                thisFragment = new ThisFragment(context);
            }
            return thisFragment;
        }
    
        @BindView(R.id.Frag_Btn_1)
        Button FragBtn1;
        @BindView(R.id.Frag_Btn_2)
        Button FragBtn2;
    
    
        Unbinder unbinder;
    
        @Nullable
        @Override
        public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.fragment_view, container, false);
            unbinder = ButterKnife.bind(this, view);  //绑定
            return super.onCreateView(inflater, container, savedInstanceState);
        }
    
        @OnClick({R.id.Frag_Btn_1, R.id.Frag_Btn_2})
        public void onViewClicked(View view) {
            switch (view.getId()) {
                case R.id.Frag_Btn_1:
                    Toast.makeText(context, "Btn_frag_lab_1", Toast.LENGTH_LONG).show();
                    break;
                case R.id.Frag_Btn_2:
                    Toast.makeText(context, "Btn_frag_lab_2", Toast.LENGTH_LONG).show();
                    break;
            }
        }
    
        @Override
        public void onDestroy() {
            super.onDestroy();
            unbinder.unbind(); // 解除绑定
        }
    }
    
    

    5.其他标签

    @BindArray 绑定string里面array数组;@BindArray(R.array.city ) String[] citys ;
    
    @BindBitmap 绑定图片资源为Bitmap;@BindBitmap( R.mipmap.wifi ) Bitmap bitmap;
    
    @BindString 绑定一个String id为一个String变量;@BindString( R.string.app_name ) String meg;
    
    @BindColor 绑定color;@BindColor(R.color.colorAccent) int black;
    
    @BindDimen 绑定Dimen;@BindDimen(R.dimen.borth_width) int mBorderWidth;
    
    @BindDrawable 绑定Drawable;@BindDrawable(R.drawable.test_pic) Drawable mTestPic;
    
    @BindFloat 绑定float
    
    @BindInt 绑定int
    
    @BindBool 绑定boolean值
    
    

    三、常见错误及其解决方法

    错误信息:

    Andorid使用Butterknife报错java.lang.NoSuchMethodError: No static method metafactory

     java.lang.NoSuchMethodError: No static method metafactory(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodHandle;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/CallSite; in class Ljava/lang/invoke/LambdaMetafactory; or its super classes (declaration of 'java.lang.invoke.LambdaMetafactory' appears in /apex/com.android.runtime/javalib/core-oj.jar)
            at butterknife.internal.DebouncingOnClickListener.<clinit>(DebouncingOnClickListener.java:12)
            at com.example.androiddemo.MainActivity_ViewBinding.<init>(MainActivity_ViewBinding.java:32)
    

    解决方法:

    将Java版本设置为1.8或更高版本。我们现在在源代码中使用Java 8功能(是lambdas),这可以让Android的编译器来处理它

    android {  
      ...
      compileOptions {  
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
      } 
    

    然后重新Sync,编译运行

    相关文章

      网友评论

          本文标题:Butterknife配置及常见错误

          本文链接:https://www.haomeiwen.com/subject/zssmahtx.html