【发布时间】:2018-04-17 10:25:00
【问题描述】:
我已经开启ButterKnife.setDebug(BuildConfig.DEBUG)
发生的情况是,一直有效的类现在找不到视图绑定。调用ButterKnife.bind(this); 后,视图仍然为空,当我检查日志时,我看到以下内容:
16401-16401 D/ButterKnife: Looking up binding for com.mobile.myapp.presentation.view.controllers.login.activities.WelcomeActivity
16401-16401 D/ButterKnife: Not found. Trying superclass com.mobile.myapp.presentation.view.base.myappBaseMvpActivity
16401-16401 D/ButterKnife: Not found. Trying superclass com.hannesdorfmann.mosby.mvp.MvpActivity
16401-16401 D/ButterKnife: Not found. Trying superclass android.support.v7.app.AppCompatActivity
16401-16401 D/ButterKnife: MISS: Reached framework class. Abandoning search.
所以我认为这可能是注释处理器依赖项。我在每个模块中添加了以下内容:
implementation 'com.jakewharton:butterknife:8.8.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
但仍然是同样的问题,没有视图得到绑定,那里是空的。我意识到我可以切换到 Kotlin,但我有很多 Java 类,并且更愿意使用 Butterknife 进行绑定。我错过了什么。这些问题发生在运行时。
这是一个空绑定的示例类:
public class WelcomeActivity extends myappBaseMvpActivity<WelcomeView, WelcomePresenter> implements WelcomeView {
@Nullable
@BindView(R.id.btn_showlogin)
Button btn_login; //this is null
@Nullable
@BindView(R.id.pager)
ViewPager mViewPager; //this is null, why ?
@Override
protected void onResumeFragments() {
super.onResumeFragments();
}
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
ButterKnife.bind(this);
mViewPager.setAdapter(new WelcomeIntroPagerAdapter(getSupportFragmentManager()));
}
//...
}
更新:我将我的代码转换为 KOTLIN 以解决这类问题,但我仍然想知道为什么 ButterKNIFE 会失败。
【问题讨论】:
-
你在对应的模块中添加了
apply plugin: 'com.jakewharton.butterknife'吗? -
我在每个模块中都添加了以下内容: implementation 'com.jakewharton:butterknife:8.8.1' annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1' 。但是我试图在我的应用程序模块中访问它的东西已经有了这种依赖关系。我尝试清理并重建。一切都很好,直到我将项目分成模块。
标签: android butterknife