重新运行应用程序后,Butterknife 8.4.0找不到视图.它得到一个NullPointerException [英] Butterknife 8.4.0 does not find views after re-running the app. It gets a NullPointerException
问题描述
在Android Studio中重新运行应用程序后,Butterknife.bind(this)
不再找到视图.到目前为止,我找到的唯一可靠的解决方案是使用Cleaning/Rebuilding Project
并再次运行它.然后它将再次查找视图,直到下一次重新运行.到目前为止,这令人难以置信,而且至少需要两分钟才能进行重建.
After re-running the app in Android Studio Butterknife.bind(this)
does not find the views anymore. The only reliable solution I found so far is by Cleaning/Rebuilding Project
and run it again. Then it finds the views again until the next re-run. This is incredibly annoying so far and takes a minimum of two minutes for a rebuild.
我有以下build.gradle
android {
compileSdkVersion 24
buildToolsVersion "25.0.0"
defaultConfig {
applicationId "xx"
minSdkVersion 21
targetSdkVersion 24
versionCode x
versionName "xxx"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
jackOptions {
enabled true
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
packagingOptions {
exclude 'META-INF/NOTICE'
exclude 'META-INF/LICENSE'
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
annotationProcessor 'com.jakewharton:butterknife-compiler:8.4.0'
compile 'com.jakewharton:butterknife:8.4.0'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
androidTestCompile 'com.android.support.test:runner:0.5'
androidTestCompile 'com.android.support:support-annotations:24.2.1'
compile 'com.android.support:appcompat-v7:24.2.1'
compile 'com.android.support:design:24.2.1'
compile 'com.android.support:support-v13:24.2.1'
compile 'com.android.support:support-v4:24.2.1'
compile 'com.android.support.test.espresso:espresso-idling-resource:2.2.2'
}
我也在使用构建工具com.android.tools.build:gradle:2.2.2
使用Butterknife.setDebug(true)
,我得到以下信息:
With Butterknife.setDebug(true)
I get the following:
D/ButterKnife: Looking up binding for xx.LoginFragment
D/ButterKnife: Not found. Trying superclass xx.BaseFragment
D/ButterKnife: Not found. Trying superclass android.app.Fragment
D/ButterKnife: MISS: Reached framework class. Abandoning search.
BaseFragment
进行绑定,而LoginFragment
对其进行扩展.
看起来像这样
The BaseFragment
does the binding and LoginFragment
extends it.
It looks like this
BaseFragment import android.app.Fragment;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(getLayoutResourceId(), container, false);
ButterKnife.setDebug(true);
unbinder = ButterKnife.bind(this, v);
initViews(v);
return v;
}
LoginFragment
@BindView(R.id.inputEmail)
protected EditText inputEmail;
@Override
protected void initViews(View v) {
EditTextFocusListener focusListener = new EditTextFocusListener();
inputEmail.setOnFocusChangeListener(focusListener);
}
还有堆栈跟踪
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.EditText.setOnFocusChangeListener(android.view.View$OnFocusChangeListener)' on a null object reference
at xx.LoginFragment.initViews(LoginFragment.java:51)
at xx.BaseFragment.onCreateView(BaseFragment.java:53)
正如我之前所说,目前唯一可靠的解决方案是对整个项目进行完整的clean/rebuild
.在使用jackCompiler
之前,这种精确的结构可以正常工作,我无法再将其禁用.代码的主要部分取决于它.
As I said before the only solution which is reliable at the moment is to do a full clean/rebuild
of the whole project. This exact structure worked fine before using the jackCompiler
and I can not disable it anymore. Major part of the code depends on it.
推荐答案
这是一个临时解决方案,直到有人有更好的答案或在稳定的频道上发布它为止.
This is a temporary solution until someone has a better answer or it's released on the stable channel.
我转到Canary频道,并将Android Studio升级到2.3 Canary和com.android.tools.build:gradle:2.3.0-alpha1
.其他所有内容均与开篇文章相同.
I changed to the canary channel and upgraded Android Studio to 2.3 Canary and com.android.tools.build:gradle:2.3.0-alpha1
. Everything else stays the same as in the opening post.
我最终决定禁用jackCompiler
并回滚到Java 1.7.现在很稳定.
I ultimately decided to disable the jackCompiler
and roll back to Java 1.7. It's stable now.
jackOptions {
enabled false
}
这篇关于重新运行应用程序后,Butterknife 8.4.0找不到视图.它得到一个NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!