JakeWharton / butterknife

Bind Android views and callbacks to fields and methods.
http://jakewharton.github.io/butterknife/
Apache License 2.0
25.55k stars 4.61k forks source link

Issue: Required view 'masked' with ID xxx for method 'login' was not found #1381

Closed voquanghoa closed 5 years ago

voquanghoa commented 5 years ago

Hello,

I'm using the ButterKnife in many Android projects, it works perfectly. However, sometimes, I got this runtime error and it makes the application crash.

java.lang.RuntimeException: Unable to start activity ComponentInfo{quanghoa.com.mailman/quanghoa.com.mailman.LoginActivity}: java.lang.IllegalStateException: Required view 'masked' with ID 2131230831 for method 'login' was not found. If this view is optional add '@Nullable' (fields) or '@Optional' (methods) annotation.
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2984)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3045)
Caused by: java.lang.IllegalStateException: Required view 'masked' with ID 2131230831 for method 'login' was not found. If this view is optional add '@Nullable' (fields) or '@Optional' (methods) annotation.
        at butterknife.internal.Utils.findRequiredView(Utils.java:92)

I'm not sure why this issue happens but it could not come from my code because if I just clean the project and rebuild again, it works. Seem it happens randomly when I made the app and deploy the app to run on phones.

Bellow is my gradle file. I'm using Kotlin with Android studio 3.1.4 on MacOS 10.13.6, gradle-4.4, no progrard, Java 8 update 181 build 13

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    ext.kotlin_version = '1.2.70'
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.1.4'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}
apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
apply plugin: 'kotlin-android-extensions'

buildscript {
    repositories {
        mavenCentral()
        maven { url 'https://maven.fabric.io/public' }
        jcenter()
        google()
    }
    dependencies {
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
        classpath 'io.fabric.tools:gradle:1.25.4'
    }
}

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "quanghoa.com.mailman"
        minSdkVersion 16
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    implementation 'com.jakewharton:butterknife:8.8.1'
    implementation 'me.dm7.barcodescanner:zbar:1.9.8'
    implementation 'org.jetbrains.anko:anko-common:0.9'

    implementation 'com.android.support:design:28.0.0'
    implementation "android.arch.persistence.room:runtime:1.1.1"

    annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
    annotationProcessor "android.arch.persistence.room:compiler:1.1.1"

    kapt "android.arch.persistence.room:compiler:1.1.1"
    kapt "com.jakewharton:butterknife-compiler:8.8.1"
    kapt "android.arch.lifecycle:compiler:1.1.1"
}

Could you check this issue and improve the library/plugin because the issue makes me waste time and it really bothers me (or maybe many other developers)

Thank you.

JakeWharton commented 5 years ago

If you can provide a sample project that demonstrates the problem I'd be happy to take a look.

Mkrizanac commented 5 years ago

@voquanghoa It's my understanding that this happens if your layout has some Views with visibility set to GONE. Similar thing was happening to me, and once I added @Optional above my @OnClick method, everything worked great, and once the view was set to VISIBLE, all onClick methods worked fine and were not troubled by that @Optional annotation.