首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >ButterKnife 8.0.1不工作

ButterKnife 8.0.1不工作
EN

Stack Overflow用户
提问于 2016-05-04 04:19:23
回答 9查看 33.2K关注 0票数 59

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

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

这是我的Main Class: (我正确地写了includes )

代码语言:javascript
运行
复制
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

代码语言:javascript
运行
复制
<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" />

EN

回答 9

Stack Overflow用户

回答已采纳

发布于 2016-05-04 04:35:52

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

代码语言:javascript
运行
复制
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是否正常工作。

票数 148
EN

Stack Overflow用户

发布于 2016-10-07 03:18:43

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

代码语言:javascript
运行
复制
ButterKnife.bind(view);

但这是错误的。库需要知道两个对象:

1)目标-带注释@BindView

2)源代码-带有视图

这样写是正确的:

代码语言:javascript
运行
复制
ButterKnife.bind(this, view);

当这个-你的片段,和这个片段的view - view。

票数 7
EN

Stack Overflow用户

发布于 2016-10-03 13:17:01

代码语言:javascript
运行
复制
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
    }
}
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37013619

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档