13

バターナイフを使用しているフラグメントがあります。

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 メソッドが呼び出されることはありません。

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>
4

4 に答える 4

19

最新のButterKnife 8.0.1 June -2016の場合

InjectViewandはandinjectに置き換えられまし た。次の手順に従います。BindViewbind

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, 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);
    }
于 2016-06-07T07:57:31.403 に答える
4

この構造を使用するだけです:

@OnClick(R.id.some_btn)
public void someButtonOnClick(View view) {
    // do something
}

これはここで説明されていますhttp://developer.android.com/reference/android/R.attr.html#onClick

于 2015-02-02T20:35:23.790 に答える
0

それでも問題が解決しない場合。プロジェクトをクリーンアップして実行してみます。修正する必要があります。それでも問題が解決しない場合は、呼び出しsomeButtonOnClick()onCreate実行し、削除してから再度実行してください。それはうまくいくはずです。それは私のために働いた。

于 2014-07-02T14:39:05.180 に答える