【问题标题】:Butterknife unable to bind views after dividing project into modulesButterknife 将项目划分为模块后无法绑定视图
【发布时间】: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


【解决方案1】:

文件-> 设置-> 插件-> Android ButterKnife Zelezny

请按照此步骤安装插件。

【讨论】:

    【解决方案2】:
    import butterknife.BindView;
    import butterknife.ButterKnife;
    import butterknife.OnClick;
    import this above class or check
    

    https://github.com/JakeWharton/butterknife#download

    【讨论】:

      【解决方案3】:

      如果你有 kotlin 依赖项,那么你应该使用 kapt 而不是 annotationProcessor

      //View Binding and Logging
      implementation 'com.jakewharton:butterknife:9.0.0-SNAPSHOT'
      kapt 'com.jakewharton:butterknife-compiler:9.0.0-SNAPSHOT'
      

      以上 if 适用于 AndroidX ,但您可以将其与您拥有的版本一起使用。只需使用 kapt! 此外,如果您已将库更新为 androidx 或更新了 gradle 插件,那么您应该使用最新版本的库! 让我知道这是否有帮助。

      【讨论】:

        猜你喜欢
        • 2016-11-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多