gpt4 book ai didi

java - butterknife @OnClick 不工作

转载 作者:行者123 更新时间:2023-11-30 10:40:41 25 4
gpt4 key购买 nike

我已经按照这里的一些解决方案来解决我的问题,但没有一个有效。我使用 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
}

依赖关系.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

最佳答案

我查看了您的原始帖子(预编辑),似乎在您的演示模块 gradle 文件中您有 apply plugin: 'com.neenbedankt.android-apt' 那应该是 应用插件:'android-apt'(参见 butterknife readme.md)。

此外,您的 gradle 中是否有此行:apt 'com.jakewharton:butterknife-compiler:8.2.1'?我看到了这个:compile presentationDependencies.butterKnife' 但当脚本运行时它不会变成 apt "com.jakewharton:butterknife:8.2.1"(即缺少“-编译器”)?

已编辑

在您的 dependencies.gradle 中,在 presentationDependencies 中添加以下行:

presentationDependencies = [    
butterKnifeApt: "com.jakewharton:butterknife-compiler:${butterKnifeVersion}",
]

然后在你的演示模块中修改:

apt presentationDependencies.butterKnife 

到:

apt presentationDependencies.butterKnifeApt

关于java - butterknife @OnClick 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38811622/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com