Seb*_*adi 59 android nullpointerexception butterknife
我正在使用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");**
}
Run Code Online (Sandbox Code Playgroud)
这是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" />
Run Code Online (Sandbox Code Playgroud)
Jak*_*ton 147
根据自述文件,您需要包含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'
}
Run Code Online (Sandbox Code Playgroud)
如果没有这个,就不会生成生成的代码,因此没有设置任何字段.
您可以通过调用ButterKnife.setDebug(true)和查看Logcat 来验证ButterKnife是否正常工作.
小智 6
我在Fragment中使用了这个库并且有NPE.我的代码是:
ButterKnife.bind(view);
Run Code Online (Sandbox Code Playgroud)
但这是错的.库需要知道两个对象:
1)目标 - 带注释@BindView
2)源 - 带有视图
这是正确的:
ButterKnife.bind(this, view);
Run Code Online (Sandbox Code Playgroud)
当这个 - 你的片段和视图 - 这个片段的视图.
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
}
}
Run Code Online (Sandbox Code Playgroud)
对我来说问题是我正在使用
annotationProcessor 'com.jakewharton:butterknife-compiler:8.7.0'
Run Code Online (Sandbox Code Playgroud)
代替
apt 'com.jakewharton:butterknife-compiler:8.7.0
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
29427 次 |
| 最近记录: |