Butterknife没有从Android库模块中查找视图

时间:2016-08-10 14:08:30

标签: android butterknife

我的项目中有3个模块:commonadminclient

第一个是带有抽象LoginActivity的Android库,其他模块(应用程序)扩展自。

LoginActivity有自己的布局,所有方法都已实现,但只有一个:tryLogin(user, password)导航到主要活动,每个应用都有不同。

我已关注official documentation但是当我点击signIn按钮时,未调用OnClickListener。此外,所有绑定视图都为空...

这是来自admin的所有相关代码(两个模块几乎相同)

项目级别build.gradle

buildscript {
    repositories {
        jcenter()
        mavenCentral()
        maven { url 'https://maven.fabric.io/public' }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.1.2'
        classpath "io.realm:realm-gradle-plugin:1.1.1"
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
        classpath 'com.jakewharton:butterknife-gradle-plugin:8.2.1'
        classpath 'io.fabric.tools:gradle:1.21.7'
    }
}

allprojects {
    repositories {
        jcenter()
        maven { url 'https://maven.fabric.io/public' }
    }
}

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

公共库build.gradle

apply plugin: 'com.android.library'
apply plugin: 'realm-android'
apply plugin: 'com.jakewharton.butterknife'
apply plugin: 'android-apt'

android {

    compileSdkVersion 24
    buildToolsVersion "24.0.1"

    defaultConfig {
        minSdkVersion 17
        targetSdkVersion 24
        versionCode 1
        versionName "0.1"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    // Local
    compile fileTree(dir: 'libs', include: ['*.jar'])

    ...

    compile 'com.jakewharton:butterknife:8.2.1'
}

管理员应用build.gradle

apply plugin: 'com.android.application'
apply plugin: 'realm-android'
apply plugin: 'io.fabric'
apply plugin: 'android-apt'

android {

    compileSdkVersion 24
    buildToolsVersion "24.0.1"

    defaultConfig {
        applicationId "..."
        minSdkVersion 17
        targetSdkVersion 24
        versionCode 1
        versionName "0.1"
    }

    buildTypes {
        debug {
            applicationIdSuffix '.debug'
        }
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {

    ...

    // Butterknife
    compile 'com.jakewharton:butterknife:8.2.1'
    apt 'com.jakewharton:butterknife-compiler:8.2.1'
}

LoginActivity(常用)

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.widget.Button;
import android.widget.EditText;

import xxx.common.R;
import xxx.common.R2;

import butterknife.BindView;
import butterknife.OnClick;

public abstract class LoginActivity extends BaseActivity {

    @BindView(R2.id.login_user_form)
    protected EditText user;

    @BindView(R2.id.login_password_form)
    protected EditText password;

    @BindView(R2.id.login_sign_in)
    protected Button signIn;

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

    @OnClick(R2.id.login_sign_in)
    protected void onSignInClick() {
        String user = this.user.toString();
        String password = this.password.getText().toString();

        /* Fields validations ... */

        tryLogin(user, password);
    }

    protected abstract void tryLogin(String user, String password);

}

activity_login.xml(常用)

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:focusableInTouchMode="true"
    android:orientation="vertical"
    android:padding="@dimen/activity_horizontal_margin">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_gravity="center"
        android:layout_weight="0.4"
        android:scaleType="fitCenter"
        android:src="@drawable/brand" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.6"
        android:gravity="center_horizontal"
        android:orientation="vertical">

        <android.support.design.widget.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <android.support.design.widget.TextInputEditText
                android:id="@+id/login_user_form"
                style="@style/LoginEditText" />

        </android.support.design.widget.TextInputLayout>

        <android.support.design.widget.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <android.support.design.widget.TextInputEditText
                android:id="@+id/login_password_form"
                style="@style/LoginEditText" />

        </android.support.design.widget.TextInputLayout>

        <Button
            android:id="@+id/login_sign_in"
            style="@style/LoginButton"
            android:text="@string/login_sign_in" />

    </LinearLayout>

</LinearLayout>

AdminLoginActivity(admin)

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;

import xxx.activity.LoginActivity;

import butterknife.ButterKnife;

public class AdminLoginActivity extends LoginActivity {

    @Override
    protected void tryLogin(String user, String password) {
        // This is never called!!
        Toast.makeText(this, "Login in...", Toast.LENGTH_SHORT).show();
    }

}

应用程序不会崩溃或任何事情,它什么都不做。

我做错了什么?

1 个答案:

答案 0 :(得分:0)

我的猜测是代码导入了错误的R文件。

要验证这一点,您可以查看在所讨论的库模块的build / generated / source中自动构建的R文件,并查看是否包含您要查找的ID。生成了几个R文件。找到具有您要查找的ID的那个,并确保它与您的R导入名称空间匹配。