2016-05-12 125 views
3

這是MainActicity:方法「onClick」在butterknife中不起作用?

package com.example.a10648.demo; 

import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.widget.Button; 
import android.widget.Toast; 

import butterknife.BindView; 
import butterknife.ButterKnife; 
import butterknife.OnClick; 

public class MainActivity extends AppCompatActivity { 


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

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

@OnClick(R.id.button) 
public void onClick() { 
    Toast.makeText(MainActivity.this,"ok",Toast.LENGTH_SHORT).show(); 
} 
} 

這是XML文件:

<?xml version="1.0" encoding="utf-8"?> 
<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" 
tools:context="com.example.a10648.demo.MainActivity"> 

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

</RelativeLayout> 

,我有 編譯 'com.jakewharton:butterknife:8.0.1'
這是的build.gradle在應用程序

apply plugin: 'com.android.application' 

android { 
compileSdkVersion 23 
buildToolsVersion "23.0.3" 

defaultConfig { 
    applicationId "com.example.a10648.demo" 
    minSdkVersion 18 
    targetSdkVersion 23 
    versionCode 1 
    versionName "1.0" 
} 
buildTypes { 
    release { 
     minifyEnabled false 
     proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
    } 
} 
} 

dependencies { 
compile fileTree(dir: 'libs', include: ['*.jar']) 
testCompile 'junit:junit:4.12' 
compile 'com.android.support:appcompat-v7:23.3.0' 
compile 'com.jakewharton:butterknife:8.0.1' 
} 

onClick在bufferknife onClick方法不起作用, 當我點擊按鈕時,沒有Toast出現? 我不知道爲什麼會發生這種情況?

+0

有任何正在添加的錯誤? – sushildlh

+1

@VishweshJainkuniya沒用。這個問題是關於butterknife –

回答

2

看起來你應該包括ButterKnife編譯器,以及:

compile 'com.jakewharton:butterknife:8.0.1' 
apt 'com.jakewharton:butterknife-compiler:8.0.1' 
7

fistly,請確保您的項目級的build.gradle如下:

buildscript { 
    repositories { 
     jcenter() 
    } 
    dependencies { 
     classpath 'com.android.tools.build:gradle:2.1.0' 
     classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8' 
    } 
} 

要特別注意這行代碼。 不要忽視!

classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8' 

然後,看看你的應用程序/的build.gradle

apply plugin: 'com.android.application' 
apply plugin: 'com.neenbedankt.android-apt' 
android { 
compileSdkVersion 23 
buildToolsVersion "23.0.3" 

defaultConfig { 
    applicationId "com.example.a10648.demo" 
    minSdkVersion 18 
    targetSdkVersion 23 
    versionCode 1 
    versionName "1.0" 
} 
buildTypes { 
    release { 
     minifyEnabled false 
     proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
    } 
} 
} 

dependencies { 
compile fileTree(dir: 'libs', include: ['*.jar']) 
testCompile 'junit:junit:4.12' 
compile 'com.android.support:appcompat-v7:23.3.0' 
compile 'com.jakewharton:butterknife:8.0.1' 
apt 'com.jakewharton:butterknife-compiler:8.0.1' 
} 

要特別注意三行代碼。 不要忽視!

apply plugin: 'com.neenbedankt.android-apt' 

compile 'com.jakewharton:butterknife:8.0.1' 
apt 'com.jakewharton:butterknife-compiler:8.0.1' 

然後,一切都好!我太高興了! 謝謝一葉飄舟 這是他在CSDN的個人頁面。

http://blog.csdn.net/jdsjlzx/article/details/51354433

+0

那麼說!沒有,但這解決了我的問題:) –

0

如果你讀的自述,你可以看到 - >

配置您的項目級的build.gradle包括了 'Android的易' 插件:

classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8' 

所以build.gradle:

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

    allprojects { 
     repositories { 
      jcenter() 
     } 
    } 

    task clean(type: Delete) { 
     delete rootProject.buildDir 
    } 

然後,應用'android-apt'插件在模塊級的build.gradle並添加黃油刀依賴關係:

apply plugin: 'android-apt' 

android { 
    ... 
} 

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

所以你的build.gradle

apply plugin: 'com.android.application' 
apply plugin: 'android-apt' 

android { 
    compileSdkVersion 23 
    buildToolsVersion "23.0.3" 

    defaultConfig { 
     applicationId "com.example.a10648.demo" 
     minSdkVersion 18 
     targetSdkVersion 23 
     versionCode 1 
     versionName "1.0" 
    } 
    buildTypes { 
     release { 
      minifyEnabled false 
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
     } 
    } 
} 


dependencies { 
    compile fileTree(dir: 'libs', include: ['*.jar']) 
    testCompile 'junit:junit:4.12' 
    compile 'com.android.support:appcompat-v7:23.3.0' 
    compile 'com.jakewharton:butterknife:8.4.0' 
    apt 'com.jakewharton:butterknife-compiler:8.4.0' 
}