2016-05-03 38 views
52

我正在使用butterknife 8.0.1,但出現了nullpointerexception黃油刀8.0.1不起作用

這行是我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" /> 

回答

141

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)驗證ButterKnife並查看Logcat。

+1

非常感謝,它作爲一種魅力! –

+0

也適合我。這應該是被接受的答案。 – PaulT

+1

我遵循相同的自述條件,但對我的工作不正常。它給了我這個'錯誤:Gradle:執行失敗的任務':app:compileWholesaleDebugJava'。 > java.lang.IllegalArgumentException:無法猜測com.sme.Activity.fragment.AccountStatementFragment' – Min2

4
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 
    } 
} 
+0

這一個爲我解決了它 – Oussaki

5

我在Fragment中使用了這個庫,並且有NPE。我的代碼是:

ButterKnife.bind(view); 

但它是錯的。圖書館需要知道兩個對象:
1)目標 - 與註解@BindView
2)來源 - 一個享有

這將是正確的寫:

ButterKnife.bind(this, view); 

當這個 - 你的片段,並視圖 - 這個片段的視圖。

+0

如果它是一項活動呢? –

0

從JakeWharton

Yes that plugin is no longer needed. You're already using annotationProcessor for the 'butterknife-compiler' artifact which is built-in to the Android Gradle plugin.

然後將溶液是刪除容易的classpath 'com.neenbedankt.gradle.plugins:Android的貼切:1.8'

1

我有這個問題,那也僅僅是因爲我從Android Studio的Dependency管理中添加了butterknife,而不是通過在Butterknife網站上覆制粘貼gradle行。 所以我不得不添加 compile 'com.jakewharton:butterknife:8.5.1' annotationProcessor 'com.jakewharton:butterknife-compiler:8.5.1' ,而不是僅僅 compile 'com.jakewharton:butterknife:8.5.1'

0
Add ` annotationProcessor 'com.jakewharton:butterknife-compiler:8.6.0'` In your gradle file it worked for me 
4

對我來說,我用

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

,而不是

apt 'com.jakewharton:butterknife-compiler:8.7.0 
0

我剛纔所面臨的問題這個問題,在我的項目更新到Gradle版本之後10。我可以通過只包括搖籃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' 
    } 

我希望這可以幫助別人,雖然這個問題是舊的。