我已经按照这里的一些解决方案解决了我的问题,但没有一个能够解决问题。我使用 Butterkinfe 实施了@OnClick
,但是当我点击它时无效。此外,如果我尝试使用setOnClickListener
手动实现侦听器,它会在bLoadDeliveres
上显示空引用,就像Butterknife没有找到它一样。
这是xml布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context=".view.activity.LoginActivity">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/bLoadDeliveries"
android:text="LOAD DELIVERIES" />
</LinearLayout>
如果我这样做,它只是工作,所以布局是好的: 这是好的
Button bLoadDeliveries;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bLoadDeliveries = (Button) findViewById(R.id.bLoadDeliveries);
if(bLoadDeliveries == null)
Toast.makeText(LoginActivity.this, "Null", Toast.LENGTH_LONG).show();
else {
bLoadDeliveries.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
navigator.navigateToDeliveryList(context);
}
});
}
}
使用Butterknife,编译和运行工作很好但是当我点击按钮时它什么都不做,就像Button和@OnClick
之间的连接完全没有完成一样。为什么它为空?为什么没有连接?
有任何想法吗?谢谢你的帮助!
这不是好的
public class LoginActivity extends BaseActivity {
@BindView(R.id.bLoadDeliveries)
Button bLoadDeliveries;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
}
// Goes to the user list screen.
@OnClick(R.id.bLoadDeliveries)
void navigateToUserList() {
Toast.makeText(LoginActivity.this, "Hola", Toast.LENGTH_SHORT).show();
this.navigator.navigateToDeliveryList(this);
}
}
已编辑:依赖文件
apply from: 'buildsystem/ci.gradle'
apply from: 'buildsystem/dependencies.gradle'
buildscript {
repositories {
jcenter()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.1.2'
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
classpath "com.fernandocejas.frodo:frodo-plugin:0.8.3"
}
}
allprojects {
ext {
androidApplicationId = 'es.app.test.presentation'
androidVersionCode = 1
androidVersionName = "1.0"
testInstrumentationRunner = "android.support.test.runner.AndroidJUnitRunner"
testApplicationId = 'es.app.test.presentation.test'
}
}
task wrapper(type: Wrapper) {
description 'Creates the gradle wrapper.'
gradleVersion '2.12'
}
task clean(type: Delete) {
delete rootProject.buildDir
}
dependencies.gradle
allprojects {
repositories {
jcenter()
}
}
ext {
//Android
androidBuildToolsVersion = "24.0.1"
androidMinSdkVersion = 23
androidTargetSdkVersion = 24
androidCompileSdkVersion = 24
//Libraries
daggerVersion = '2.5'
butterKnifeVersion = '8.2.1'
recyclerViewVersion = '24.1.1'
rxJavaVersion = '1.1.8'
rxAndroidVersion = '1.2.1'
javaxAnnotationVersion = '1.0'
javaxInjectVersion = '1'
gsonVersion = '2.4'
okHttpVersion = '3.4.1'
androidAnnotationsVersion = '24.1.1'
sqliteAssetHelperVersion = '2.0.1'
//Testing
robolectricVersion = '3.0'
jUnitVersion = '4.12'
assertJVersion = '1.7.1'
mockitoVersion = '1.9.5'
dexmakerVersion = '1.0'
espressoVersion = '2.0'
testingSupportLibVersion = '0.1'
//Development
leakCanaryVersion = '1.3.1'
presentationDependencies = [
daggerCompiler: "com.google.dagger:dagger-compiler:${daggerVersion}",
dagger: "com.google.dagger:dagger:${daggerVersion}",
butterKnife: "com.jakewharton:butterknife:${butterKnifeVersion}",
recyclerView: "com.android.support:recyclerview-v7:${recyclerViewVersion}",
rxJava: "io.reactivex:rxjava:${rxJavaVersion}",
rxAndroid: "io.reactivex:rxandroid:${rxAndroidVersion}",
javaxAnnotation: "javax.annotation:jsr250-api:${javaxAnnotationVersion}",
]
presentationTestDependencies = [
mockito: "org.mockito:mockito-core:${mockitoVersion}",
dexmaker: "com.google.dexmaker:dexmaker:${dexmakerVersion}",
dexmakerMockito: "com.google.dexmaker:dexmaker-mockito:${dexmakerVersion}",
espresso: "com.android.support.test.espresso:espresso-core:${espressoVersion}",
testingSupportLib: "com.android.support.test:testing-support-lib:${testingSupportLibVersion}",
]
domainDependencies = [
daggerCompiler: "com.google.dagger:dagger-compiler:${daggerVersion}",
dagger: "com.google.dagger:dagger:${daggerVersion}",
javaxAnnotation: "javax.annotation:jsr250-api:${javaxAnnotationVersion}",
javaxInject: "javax.inject:javax.inject:${javaxInjectVersion}",
rxJava: "io.reactivex:rxjava:${rxJavaVersion}",
]
domainTestDependencies = [
junit: "junit:junit:${jUnitVersion}",
mockito: "org.mockito:mockito-core:${mockitoVersion}",
]
dataDependencies = [
daggerCompiler: "com.google.dagger:dagger-compiler:${daggerVersion}",
dagger: "com.google.dagger:dagger:${daggerVersion}",
okHttp: "com.squareup.okhttp3:okhttp:${okHttpVersion}",
gson: "com.google.code.gson:gson:${gsonVersion}",
rxJava: "io.reactivex:rxjava:${rxJavaVersion}",
rxAndroid: "io.reactivex:rxandroid:${rxAndroidVersion}",
javaxAnnotation: "javax.annotation:jsr250-api:${javaxAnnotationVersion}",
javaxInject: "javax.inject:javax.inject:${javaxInjectVersion}",
androidAnnotations: "com.android.support:support-annotations:${androidAnnotationsVersion}",
sqliteAssetHelper: "com.readystatesoftware.sqliteasset:sqliteassethelper:${sqliteAssetHelperVersion}"
]
dataTestDependencies = [
junit: "junit:junit:${jUnitVersion}",
assertj: "org.assertj:assertj-core:${assertJVersion}",
mockito: "org.mockito:mockito-core:${mockitoVersion}",
robolectric: "org.robolectric:robolectric:${robolectricVersion}",
]
developmentDependencies = [
leakCanary: "com.squareup.leakcanary:leakcanary-android:${leakCanaryVersion}",
]
}
演示模块
apply plugin: 'com.android.application'
apply plugin: 'android-apt'
apply plugin: 'com.fernandocejas.frodo'
frodo {
enabled = true
}
android {
def globalConfiguration = rootProject.extensions.getByName("ext")
compileSdkVersion globalConfiguration.getAt("androidCompileSdkVersion")
buildToolsVersion globalConfiguration.getAt("androidBuildToolsVersion")
defaultConfig {
minSdkVersion globalConfiguration.getAt("androidMinSdkVersion")
targetSdkVersion globalConfiguration.getAt("androidTargetSdkVersion")
applicationId globalConfiguration.getAt("androidApplicationId")
versionCode globalConfiguration.getAt("androidVersionCode")
versionName globalConfiguration.getAt("androidVersionName")
testInstrumentationRunner globalConfiguration.getAt("testInstrumentationRunner")
testApplicationId globalConfiguration.getAt("testApplicationId")
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
packagingOptions {
exclude 'LICENSE.txt'
exclude 'META-INF/DEPENDENCIES'
exclude 'META-INF/ASL2.0'
exclude 'META-INF/NOTICE'
exclude 'META-INF/LICENSE'
}
lintOptions {
quiet true
abortOnError false
ignoreWarnings true
disable 'InvalidPackage' //Some libraries have issues with this.
disable 'OldTargetApi' //Lint gives this warning but SDK 20 would be Android L Beta.
disable 'IconDensities' //For testing purpose. This is safe to remove.
disable 'IconMissingDensityFolder' //For testing purpose. This is safe to remove.
}
signingConfigs {
debug {
storeFile file('../buildsystem/debug.keystore')
storePassword 'android'
keyAlias 'androiddebugkey'
keyPassword 'android'
}
}
/* buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
*/
buildTypes {
debug {
signingConfig signingConfigs.debug
}
}
}
dependencies {
def presentationDependencies = rootProject.ext.presentationDependencies
def presentationTestDependencies = rootProject.ext.presentationTestDependencies
def developmentDependencies = rootProject.ext.developmentDependencies
compile project(':domain')
compile project(':data')
compile 'com.android.support:appcompat-v7:24.1.1'
apt presentationDependencies.daggerCompiler
compile presentationDependencies.dagger
compile presentationDependencies.butterKnife
apt presentationDependencies.butterKnife
compile presentationDependencies.recyclerView
compile presentationDependencies.rxJava
compile presentationDependencies.rxAndroid
provided presentationDependencies.javaxAnnotation
androidTestCompile presentationTestDependencies.mockito
androidTestCompile presentationTestDependencies.dexmaker
androidTestCompile presentationTestDependencies.dexmakerMockito
androidTestCompile presentationTestDependencies.espresso
androidTestCompile presentationTestDependencies.testingSupportLib
//Development
compile developmentDependencies.leakCanary
答案 0 :(得分:1)
我查看了您的原始帖子(预编辑),似乎在您的演示模块gradle文件中,您有apply plugin: 'com.neenbedankt.android-apt'
那应该是apply plugin: 'android-apt'
(请参阅butterknife readme.md)。< / p>
此外,您是否在任何地方都有这条线:apt 'com.jakewharton:butterknife-compiler:8.2.1'
?我看到了这个:compile presentationDependencies.butterKnife'
但是当脚本运行时(即缺少“-compiler”),它会不会变为apt "com.jakewharton:butterknife:8.2.1"
?
<强> EDITED 强>
在dependencies.gradle中,在presentationDependencies中添加以下行:
presentationDependencies = [
butterKnifeApt: "com.jakewharton:butterknife-compiler:${butterKnifeVersion}",
]
然后在您的演示模块中修改:
apt presentationDependencies.butterKnife
为:
apt presentationDependencies.butterKnifeApt