Butterknife片段按钮不起作用

Per*_*tra 13 android fragment butterknife

我有一个片段,我正在使用Butterknife.

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
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,在屏幕上点击按钮时,永远不会调用someButtonOnClick方法.

关于Butterknife框架我做错了什么的想法?

这是正在使用的布局:

<?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>
Run Code Online (Sandbox Code Playgroud)

Hit*_*ahu 19

最新的ButterKnife 8.0.1 2016年6月

InjectViewinject已被替换为BindViewbind 以下步骤: -

down vote unaccept来自Butterknife github页面:

将其添加到项目级build.gradle:

buildscript {
  repositories {
    mavenCentral()
   }
  dependencies {
    classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
  }
}
Run Code Online (Sandbox Code Playgroud)

将其添加到模块级build.gradle:

apply plugin: 'android-apt'

android {
  ...
}

dependencies {
  compile 'com.jakewharton:butterknife:8.0.1'
  apt 'com.jakewharton:butterknife-compiler:8.0.1'
}
Run Code Online (Sandbox Code Playgroud)

然后像这样使用Butterknife

 private Unbinder unbinder;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View calcView = inflater.inflate(R.layout.content_main, container, 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);
    }
Run Code Online (Sandbox Code Playgroud)

  • Ups,我忘了添加mavenCentral(). (2认同)

dhi*_*iku 0

如果您仍有问题。清理项目并尝试运行。它应该修复。如果仍然有问题,请调用someButtonOnClick()onCreate运行并删除它,然后再次运行它。它应该有效。这对我有用。