我有一个片段,我正在使用面包刀.
public class FooFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) { View view = inflater.inflate(R.layout.foo,container,false); ButterKnife.inject(this,view); return view; } @OnClick(R.id.some_btn) public void someButtonOnClick() { // do something } }
但是,当按钮在屏幕上轻击时,一直不会调用someButtonOnClick方法.
想想我在做什么错误的面包框架?
这是正在使用的布局:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="@color/White"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/some_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:layout_margin="@dimen/xsmall_margin" android:background="@drawable/some_btn_selection" /> <Button android:id="@+id/some_other_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:layout_margin="@dimen/xsmall_margin" android:background="@drawable/some_other_btn_selection" /> </LinearLayout> </LinearLayout>
解决方法
最新的ButterKnife 8.0.1 6月-2016
InjectView和注入已被BindView替换并绑定
这是以下步骤: –
投票
取消接受
从Butterknife github页面:
将其添加到项目级build.gradle中:
buildscript { repositories { mavenCentral() } dependencies { classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8' } }
将其添加到您的模块级build.gradle中:
apply plugin: 'android-apt' android { ... } dependencies { compile 'com.jakewharton:butterknife:8.0.1' apt 'com.jakewharton:butterknife-compiler:8.0.1' }
然后用这样的面包刀
private Unbinder unbinder; @Override public View onCreateView(LayoutInflater inflater,Bundle savedInstanceState) { // Inflate the layout for this fragment View calcView = inflater.inflate(R.layout.content_main,false); unbinder = ButterKnife.bind(this,calcView); return calcView; } @Override public void onDestroyView() { super.onDestroyView(); unbinder.unbind(); } @OnClick(R.id.one) public void one() { Toast.makeText(getActivity(),"Working",Toast.LENGTH_SHORT).show(); result.setText(result.getText().toString() + 1); }