2016-05-05 9 views
3

アドバタイズ:http://jakewharton.github.io/butterknife/ 私は例のコードを貼り付けて、私のRelativeLayoutファイルを使用してレイアウトを挿入:バターナイフビューホルダーパターンは、私はドキュメントのようなカスタムアダプタにバターナイフを使用しようとしている

@Override public View getView(int position, View view, ViewGroup parent) { 
    ViewHolder holder; 
    ButterKnife.setDebug(true); 
    if (view != null) { 
     holder = (ViewHolder) view.getTag(); 
    } else { 
     view = inflater.inflate(R.layout.list_row_people, parent, false); 
     holder = new ViewHolder(view); 
     view.setTag(holder); 
    } 

    holder.name.setText("John Doe"); 
    // etc... 

    return view; 
    } 

これはViewHolder(PeopleAdapterのインラインクラス)のコードである:nameをnとして

static class ViewHolder { 
    @BindView(R.id.name) 
    TextView name; 

    public ViewHolder(View view) { 
     ButterKnife.bind(this, view); 
    } 
} 

残念ながら、holder.name.setText("John Doe");がnullpointer例外をスローull。デバッグ出力には、これらの行を示しています

13:11:32.816 11613-11613/com.myproject.debug D/ButterKnife: Looking up view binder for com.myproject.controller.adapters.PeopleAdapter$ViewHolder 
13:11:32.827 11613-11613/com.myproject.debug D/ButterKnife: Not found. Trying superclass java.lang.Object 
13:11:32.827 11613-11613/com.myproject.debug D/ButterKnife: MISS: Reached framework class. Abandoning search. 

list_row_people.xmlは現在、次のようになります。

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical"> 

    <TextView 
     android:id="@+id/name" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:gravity="center_vertical" 
     android:padding="4dp" 
     android:text="@string/example_name" 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 
</RelativeLayout> 

https://github.com/JakeWharton/butterknife/issues/285を読んで、私は私のproguard-rules.proに次の行を追加しました:

-keep class **$$ViewBinder { *; } 

編集:

Androidスタジオ2.1 with com.jakewharton:バナーナイフ:8.0.1。これは私のbuild.gradleです:バージョン8.0.0からバターナイフで

apply plugin: 'com.android.application' 
apply plugin: 'android-apt' 
apply plugin: 'kotlin-android' 

repositories { 
    mavenCentral() 
} 

android { 
    compileSdkVersion 23 
    buildToolsVersion "23.0.3" 
    defaultConfig { 
     applicationId "com.myproject" 
     minSdkVersion 18 
     targetSdkVersion 23 
     versionCode 18 
     versionName "0.0.16" 
     multiDexEnabled true 
    } 
    packagingOptions { 
     exclude 'main/AndroidManifest.xml' 
    } 
    buildTypes { 
     release { 
      minifyEnabled false 
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
     } 
     debug { 
      applicationIdSuffix ".debug" 
     } 
    } 
    lintOptions { 
     abortOnError false 
     warning 'InvalidPackage' 
    } 
    sourceSets { 
     main.java.srcDirs += 'src/main/kotlin' 
    } 
    compileOptions { 
     targetCompatibility JavaVersion.VERSION_1_7 
     sourceCompatibility JavaVersion.VERSION_1_7 
    } 
} 

dependencies { 
    compile fileTree(include: ['*.jar'], dir: 'libs') 
    wearApp project(':wear') 
    testCompile 'junit:junit:4.12' 
    apt 'com.github.hotchemi:permissionsdispatcher-processor:2.1.2' 
    releaseCompile project(path: ':common', configuration: 'release') 
    debugCompile project(path: ':common', configuration: 'debug') 
    compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" 
    compile 'com.google.android.gms:play-services-maps:8.4.0' 
    compile 'com.google.maps.android:android-maps-utils:0.4.3' 
    compile 'com.google.android.gms:play-services-wearable:8.4.0' 
    compile 'com.android.support:appcompat-v7:23.3.0' 
    compile 'com.android.support:support-v4:23.3.0' 
    compile 'com.android.support:design:23.3.0' 
    compile 'com.android.support:recyclerview-v7:23.3.0' 
    compile 'com.google.code.gson:gson:2.6.2' 
    compile 'com.jakewharton.timber:timber:2.5.0' 
    compile 'com.jakewharton:butterknife:8.0.1' 
    compile 'com.github.hotchemi:permissionsdispatcher:2.1.2' 
    compile 'com.squareup.picasso:picasso:2.5.2' 
} 
buildscript { 
    ext.kotlin_version = '1.0.1-2' 
    repositories { 
     mavenCentral() 
    } 
    dependencies { 
     classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" 
    } 
} 
+1

あなたは 'Android Studio'を使用していますか?もしあなたが 'ButterKnife'の' Version'を使用していたら?あなたの 'Gradle'を見ることができますか? –

+0

私の答えをチェックしてください。 –

+0

@ piotr.wittchenあなたのコメントは何ですか? –

答えて

7

Runtimecompiler今2つのアーティファクトに分割されています。

compile 'com.jakewharton:butterknife:8.0.0' 
apt 'com.jakewharton:butterknife-compiler:8.0.0' 

もこのLinkを見てください。

+1

ニース、ありがとう!私のコードはうまくいきました。答えに書かれているように 'build.gradle'を更新するだけでした。 – user3105453

関連する問題