Butterknife Fragment Button not working

♀尐吖头ヾ 提交于 2019-11-30 17:49:27

For latest ButterKnife 8.0.1 June -2016

InjectView and inject has been replaced with BindView and bind This are the steps to follow:-

down vote unaccept From the Butterknife github page:

Add this to you project-level build.gradle:

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

Add this to your module-level build.gradle:

apply plugin: 'android-apt'

android {
  ...
}

dependencies {
  compile 'com.jakewharton:butterknife:8.0.1'
  apt 'com.jakewharton:butterknife-compiler:8.0.1'
}

And then use Butterknife like this

 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);
    }

Just use this structure:

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

This is described here http://developer.android.com/reference/android/R.attr.html#onClick

This works for me:

@Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_home, container, false);
    ButterKnife.bind(getActivity(), view);
    return view;
}
dhiku

If you are still having issue. Clean the project and try to run. It should fix. If you still have issue, call someButtonOnClick() in onCreate and run and remove it and again run it. It should work. It worked for me.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!