@OnClick is not working in implementation of ButterKnife Library

泄露秘密 提交于 2020-01-03 13:57:06

问题


@OnClick is not working in implementation of ButterKnife Library

When I click on the Button, nothing is happening.

This is my full code:

public class MainActivity extends ActionBarActivity {
    @InjectView(R.id.edit_user)
    EditText username;
    @InjectView(R.id.edit_pass)
    EditText password;

    @OnClick(R.id.btn)
    void submit() {
        // TODO call server...
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.inject(this);
        // TODO Use "injected" views...
    }
}

This is my xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<EditText
    android:id="@+id/edit_user"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:hint="user" />

<EditText
    android:id="@+id/edit_pass"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:hint="user" />

<Button
    android:id="@+id/btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button" />

Thanks


回答1:


As mentioned in the Butterknife docs, If you are using Eclipse, you will need to configure the IDE before the annotations will be processed




回答2:


For anyone running into this issue in Android Studio, make sure you are including both of the necessary dependencies and the apt plugin in your respective build files (check the Butterknife readme). I rushed through the docs and only included the compile dependency, which caused binding to fail silently.




回答3:


In your activity try to add..

 ButterKnife.inject(this);

check this code

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.inject(this);
}

@OnClick(R.id.buttonAlert)
public void alertClicked(View v){
new AlertDialog.Builder(v.getContext())
    .setMessage(getFormattedMessge())
    .setNeutralButton("OK", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
        dialog.dismiss();
        }
    })
    .show();
 }



回答4:


Double check all dependencies in your project. Here is download instructions from readme file. Configure your project-level build.gradle to include the 'android-apt' plugin:

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

Then, apply the 'android-apt' plugin in your module-level build.gradle and add the Butter Knife dependencies:

apply plugin: 'android-apt'

android {
  ...
}

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

Note: If you are using the new Jack compiler with version 2.2.0 or newer you do not need the 'android-apt' plugin and can instead replace apt with annotationProcessor when declaring the compiler dependency.




回答5:


Use ButterKnife.bind(this); in onCreate() of an Activity. or onCreateView for Fragment.

@OnClick(R.id.button_stop_sticky)
    public void onStopClicked(View v) {

        Toast.makeText(this, "onStop Clicked", Toast.LENGTH_LONG).show();

    }

And obviously, app module > gradle add dependency

compile 'com.jakewharton:butterknife:8.8.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'



回答6:


Using Butter Knife you can bind your view like this also...

 class ExampleActivity extends Activity {

    @Bind(R.id.title)
    TextView title;
    @Bind(R.id.subtitle)
    TextView subtitle;
    @Bind(R.id.footer)
    TextView footer;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.simple_activity);
        ButterKnife.bind(this);
        // TODO Use fields...
    }
}

for more details you can see this link http://jakewharton.github.io/butterknife/



来源:https://stackoverflow.com/questions/27754811/onclick-is-not-working-in-implementation-of-butterknife-library

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