使用ButterKnife无法inject view的解决办法

使用ButterKnife做android开发时,发现无法inject,如下,tvInfo总是null。

	@InjectView(R.id.textView1Info) TextView tvInfo;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getWindow().requestFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.main);
		
		ButterKnife.inject(this);
		tvInfo.setText("test");// tvInfo is null.
	}

 

后来在官网上找到解决办法,如下:

http://jakewharton.github.io/butterknife/ide-eclipse.html

 

Eclipse Configuration

  1. Right click on your project in the Package Explorer. Head to Java Compiler → Annotation Processing and check "Enable project specific settings".

     

    Ensure the other annotation processing settings are the same as shown below:

  2. Expand the Annotation Processing section and select Factory Path. Check "Enable project specific settings" and then click "Add JARs…". Navigate to the project's libs/ folder and select the Butter Knife jar.

  3. Click "Ok" to save the new settings. Eclipse will ask you to rebuild your project to which you should click "Yes"
  4. Make sure that the .apt_generated/ folder is in your project root. It should contain files like YOURACTIVITY$$ViewInjector.java. If these files are not present trigger a clean build by selected Project → Clean. This folder and files should not be checked into revision control.
  5. Lastly, under "Java Compiler", make sure that the Compiler compliance level is set to Java version 1.6 at minimum.

转载于:https://www.cnblogs.com/yahb/p/4201275.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用ButterKnife,需要进行以下步骤: 1. 在项目的build.gradle文件中添加ButterKnife的依赖: ```groovy dependencies { implementation 'com.jakewharton:butterknife:10.2.3' annotationProcessor 'com.jakewharton:butterknife-compiler:10.2.3' } ``` 2. 在需要使用ButterKnife的Activity或Fragment中,使用`@BindView`注解来绑定视图元素。 ```java public class MainActivity extends AppCompatActivity { @BindView(R.id.textView) TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ButterKnife.bind(this); // 现在可以直接使用textView了 textView.setText("Hello ButterKnife!"); } } ``` 3. 调用`ButterKnife.bind(this)`方法来完成视图的绑定。这通常是在Activity的`onCreate()`方法中进行。 4. 在需要为视图设置点击事件、长按事件等的地方,使用`@OnClick`、`@OnLongClick`等注解来定义相应的事件处理方法。 ```java public class MainActivity extends AppCompatActivity { @BindView(R.id.button) Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ButterKnife.bind(this); // 设置点击事件处理方法 button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 处理点击事件 } }); } @OnClick(R.id.button) void onButtonClick() { // 处理点击事件,使用注解方式 } @OnLongClick(R.id.button) boolean onButtonLongClick() { // 处理长按事件,使用注解方式 return true; } } ``` 这样,你就可以使用ButterKnife来简化视图绑定和事件处理的工作了。记得在合适的时机调用`ButterKnife.unbind(this)`方法来解绑视图。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值