(Android Butterknife)点击

时间:2017-11-11 08:52:30

标签: android butterknife

我有一个按钮,我想在点击它后更改它上面的文字。

@BindView(R.id.btn) Button btn;

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

} 
@OnClick(R.id.btn)
public void click()
{
    btn.setText("anything");
}

我已经意识到我可以使用普通的onclickListener来做到这一点,但我想知道我是否可以使用butterknife来做到这一点。

修改

摇篮

apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.0"
    defaultConfig {
        applicationId "com.jnx.test"
        minSdkVersion 14
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.3.0'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    testCompile 'junit:junit:4.12'


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

修改

错误日志

 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.jnx.test/com.jnx.test.MainActivity}: java.lang.IllegalStateException: Required view 'btn' with ID 2131427422 for field 'btn' and method 'click' was not found. If this view is optional add '@Nullable' (fields) or '@Optional' (methods) annotation.
                                                                    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                                                                    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                                    at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                                                                    at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                    at android.os.Looper.loop(Looper.java:148)
                                                                    at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                    at java.lang.reflect.Method.invoke(Native Method)
                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                 Caused by: java.lang.IllegalStateException: Required view 'btn' with ID 2131427422 for field 'btn' and method 'click' was not found. If this view is optional add '@Nullable' (fields) or '@Optional' (methods) annotation.
                                                                    at butterknife.internal.Utils.findRequiredView(Utils.java:92)
                                                                    at com.jnx.test.MainActivity_ViewBinding.<init>(MainActivity_ViewBinding.java:29)
                                                                    at java.lang.reflect.Constructor.newInstance(Native Method)
                                                                    at butterknife.ButterKnife.createBinding(ButterKnife.java:199)
                                                                    at butterknife.ButterKnife.bind(ButterKnife.java:124)
                                                                    at com.jnx.test.MainActivity.onCreate(MainActivity.java:22)

MainActivity中的第22行是setContentView(R.layout.activity_main);

2 个答案:

答案 0 :(得分:0)

您可以传递视图:

@OnClick(R.id.btn)
public void click(Button button) {
    button.setText("Hello");
}

你不需要这一行,所以删除它:

@BindView(R.id.btn) Button btn;

答案 1 :(得分:0)

好的,我明白了。我认为之前有人发布了这样的答案,但起初它并没有给我带来帮助。它在android studio重新启动后开始工作。

@BindView(R.id.btn) Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ButterKnife.bind(this);

}
@OnClick(R.id.btn)
public void click(View v)
{
    btn.setText("dsdssd");
}

您必须将View作为参数传递。