【问题标题】:ButterKnife 8.0.1 not workingButterKnife 8.0.1 不工作
【发布时间】:2016-08-29 01:24:25
【问题描述】:

我正在使用butterknife 8.0.1,但出现了nullpointerexception

这一行在我的 build.grade 文件中: compile 'com.jakewharton:butterknife:8.0.1'

这是我的Main Class:(我正确地编写了包含)

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends BaseActivity {

    @BindView(R.id.MainScreenTextView) TextView mainText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ButterKnife.bind(this);

        **mainText.setText("Butter knife is working fine");**
    }

这是MainActivity.xml:

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_vertical"
    android:orientation="vertical">

    <TextView
        android:id="@+id/MainScreenTextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="This is the Main Screen"
        android:textColor="#000000"
        android:background="#666666"
        android:padding="5dp"
        android:textSize="20dp"/>
</LinearLayout>

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_margin="@dimen/fab_margin"
    android:src="@android:drawable/ic_dialog_email" />

【问题讨论】:

    标签: android nullpointerexception butterknife


    【解决方案1】:

    根据the readme,您需要包含butterknife-compiler 才能自动生成生成的代码:

    buildscript {
      repositories {
        mavenCentral()
       }
      dependencies {
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
      }
    }
    
    apply plugin: 'com.neenbedankt.android-apt'
    
    dependencies {
      compile 'com.jakewharton:butterknife:8.0.1'
      apt 'com.jakewharton:butterknife-compiler:8.0.1'
    }
    

    如果没有这个,就不会加载任何生成的代码,因此不会设置任何字段。

    您可以通过调用 ButterKnife.setDebug(true) 并查看 Logcat 来验证 ButterKnife 是否正常工作。

    【讨论】:

    • 对我也很有效。这应该是公认的答案。
    • 我遵循了相同的自述条件,但对我来说它不起作用。它给了我这个Error:Gradle: Execution failed for task ':app:compileWholesaleDebugJava'. &gt; java.lang.IllegalArgumentException: couldn't make a guess for com.sme.Activity.fragment.AccountStatementFragment
    • 作者为什么要搞得这么复杂?
    • @KaiWang 因为减少代码中的反射从而提高执行速度30%
    • 仍然收到NPA
    【解决方案2】:

    我在 Fragment 中使用了这个库并且有 NPE。我的代码是:

    ButterKnife.bind(view);
    

    但这是错误的。库需要知道两个对象:
    1) 目标 - 带有注释 @BindView
    2) 来源 - 带视图

    这样写就对了:

    ButterKnife.bind(this, view);
    

    当这个 - 你的片段和视图 - 这个片段的视图。

    【讨论】:

    • 如果是活动呢?
    【解决方案3】:
    App Level(build.gradle)
    
    apply plugin: 'android-apt'
    dependencies {
        compile fileTree(include: ['*.jar'], dir: 'libs')
        compile 'com.android.support:appcompat-v7:24.2.1'
        testCompile 'junit:junit:4.12'
        compile 'com.jakewharton:butterknife:8.4.0'
        apt 'com.jakewharton:butterknife-compiler:8.4.0'
    }
    
    
    Project Level(build.gradle)
    
    buildscript {
        repositories {
            jcenter()
        }
        dependencies {
    
            classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
            // NOTE: Do not place your application dependencies here; they belong
            // in the individual module build.gradle files
        }
    }
    

    【讨论】:

      【解决方案4】:

      对我来说问题是我正在使用

      annotationProcessor 'com.jakewharton:butterknife-compiler:8.7.0'
      

      而不是

       apt 'com.jakewharton:butterknife-compiler:8.7.0
      

      【讨论】:

      • 有什么区别?
      【解决方案5】:

      像这样在 build.gradle 文件上配置 Butterknife,

      compile("com.jakewharton:butterknife:8.5.1")
      annotationProcessor "com.jakewharton:butterknife-compiler:8.5.1"
      

      它对我有用。

      【讨论】:

        【解决方案6】:

        我也有这个问题,只是因为我从 Android Studio 的依赖管理中添加了黄油刀,而不是通过从黄油刀网站复制粘贴 gradle 行。 所以我不得不添加 compile 'com.jakewharton:butterknife:8.5.1' annotationProcessor 'com.jakewharton:butterknife-compiler:8.5.1' 而不仅仅是 compile 'com.jakewharton:butterknife:8.5.1'

        【讨论】:

          【解决方案7】:

          如果你使用 kotlin:

          确保在模块应用中使用此依赖项:

          dependencies {
             implementation 'com.jakewharton:butterknife:10.0.0'
             kapt 'com.jakewharton:butterknife-compiler:10.0.0'
          }
          

          【讨论】:

          • 这是我的问题。此外,即使您的项目中的 kotlin 代码为零,但使用并删除了 Kotlin sn-ps,除非包含上面的 kapt 行,否则您仍然会遇到错误。
          【解决方案8】:

          来自杰克沃顿

          是的,不再需要该插件。你已经在使用 'butterknife-compiler' 工件的 annotationProcessor 是 内置于 Android Gradle 插件中。

          那么解决办法就是删除apt classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'

          【讨论】:

            【解决方案9】:

            在将我的项目更新到 Gradle 版本 3.0.1 后,我刚刚遇到了这个问题。我可以通过在 Gradle app 文件中包含以下行来修复它:

            annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
            

            最终结果是:

                dependencies {
                    compile fileTree(include: ['*.jar'], dir: 'libs')
                    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
                        exclude group: 'com.android.support', module: 'support-annotations'
                    })
            
                    ...
            
                    compile 'com.jakewharton:butterknife:8.8.1'
                    annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
                }
            

            我希望这对某人有所帮助,尽管这个问题很老。

            【讨论】:

              【解决方案10】:

              在你的 gradle 文件中添加 annotationProcessor 'com.jakewharton:butterknife-compiler:8.6.0' 它对我有用

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 2018-05-09
                • 2016-11-15
                • 1970-01-01
                • 2020-01-20
                • 2015-10-26
                • 2015-04-15
                相关资源
                最近更新 更多