The use and precautions of ButterKnife in Android

add dependencies

implementation 'com.jakewharton:butterknife:10.2.3' 
annotationProcessor 'com.jakewharton:butterknife-compiler:10.2.3'

use:

    @BindView(R.id.font_dec_img)
    ImageView fontDec;
    @BindView(R.id.font_add_img)
    ImageView fontAdd;
    @BindView(R.id.back_rlv)
    RelativeLayout back;
    @BindView(R.id.clean_rl)
    RelativeLayout clean;
    @BindView(R.id.save_rl)
    RelativeLayout save;
    @BindView(R.id.edit_cb)
    CheckBox edit;
    @BindView(R.id.text_edt)
    EditText text;

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

    }

  @OnClick({R.id.font_dec_img,R.id.font_add_img,R.id.edit_cb,R.id.save_rl,R.id.clean_rl,R.id.back_rlv})
        public void onViewClick(View view){
            switch (view.getId()){
                case R.id.font_dec_img:
                   //字号小
                    break;
                case R.id.font_add_img:
                    //字号加
                    break;
                case R.id.edit_cb:
                    //编辑框
                    break;
                case R.id.save_rl:
                    //保存内容
                    break;
                case R.id.clean_rl:
                    //清除内容
                    text.setText("");
                    break;
                case R.id.back_rlv:
                    //退出软件
                    finish();
                    break;
            }
        }

question:

At the beginning, the reference to the dependency only added the implementation 'com.jakewharton:butterknife:10.2.3' line, and then an error was reported when running, a null pointer exception, and the reason why the control was not initialized. I have been looking for the problem for a long time, and it is a reference dependency error. or missing issues

annotationProcessor 'com.jakewharton:butterknife-compiler:10.2.3' just add this line

Guess you like

Origin blog.csdn.net/m0_56366502/article/details/128865119