问题描述
我正在使用 butterknife 8.0.1
,但是出现了 nullpointerexception
.
I am using butterknife 8.0.1
, but a nullpointerexception
is appearing.
这一行在我的 build.grade 文件中:编译'com.jakewharton:butterknife:8.0.1'
This line is on my build.grade file: compile 'com.jakewharton:butterknife:8.0.1'
这是我的主类:
(我正确地编写了包含)
this is my Main Class:
(I wrote the includes properly)
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" />
推荐答案
根据 自述文件,您需要包含butterknife-compiler
为了自动生成生成的代码:
Per the readme, you need to include the butterknife-compiler
in order for the generated code to be produced automatically:
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'
}
没有这个就没有要加载的生成代码,因此没有任何字段被设置.
Without this there is no generated code to be loaded and thus none of the fields get set.
您可以通过调用 ButterKnife.setDebug(true)
并查看 Logcat 来验证 ButterKnife 是否正常工作.
You can verify ButterKnife is working by calling ButterKnife.setDebug(true)
and looking in Logcat.
这篇关于ButterKnife 8.0.1 不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!