Butterknife 8.4.0 在重新运行应用程序后找不到视图。它得到一个空点异常
在Android Studio中重新运行应用程序后,不再找到视图。到目前为止,我发现的唯一可靠的解决方案是再次运行它。然后,它再次查找视图,直到下次重新运行。到目前为止,这非常烦人,并且至少需要两分钟的时间来重建。Butterknife.bind(this)
Cleaning/Rebuilding Project
我有以下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)
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
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