【问题标题】:ButterKnife: Not found. Trying superclass android.support.v7.app.AppCompatActivity黄油刀:没有找到。尝试超类 android.support.v7.app.AppCompatActivity
【发布时间】:2016-09-25 09:19:53
【问题描述】:

我正在尝试使用 Butterknife 绑定 TextView 我的主要活动和布局由 ID 为 tv_app_name 的 TextView 组成。我从documentation 关注。

public class MainActivity extends AppCompatActivity {

@BindView(R.id.tv_app_name)
TextView tvAppName;

@BindString(R.string.app_name)
String appName;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ButterKnife.bind(this);

    ButterKnife.setDebug(true);

    tvAppName.setText(appName);


 }
}

我遇到了一个问题,即 tvAppName 未初始化并显示 nullPointerException。我添加了 ButterKnife.setDebug(true); 正如 StackOverflow 的一些答案中所建议的那样,我在日志中发现了 ButterKnife: Not found. Trying superclass android.support.v7.app.AppCompatActivity

在我寻找解决此问题的方法时。有人说添加依赖butterknife-compiler,但我已经添加了:

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.android.support:appcompat-v7:24.2.1'
    testCompile 'junit:junit:4.12'

    //butterknife
    compile 'com.jakewharton:butterknife:8.4.0'
    apk 'com.jakewharton:butterknife-compiler:8.4.0'
}

什么是问题bihinde:

黄油刀:未找到。尝试超类 android.support.v7.app.AppCompatActivity 以及如何解决这个问题?

【问题讨论】:

    标签: android butterknife null-pointer


    【解决方案1】:

    您的依赖项中有错字。 build.gradle 文件中的行

    apk 'com.jakewharton:butterknife-compiler:8.4.0'

    应该是

    apt 'com.jakewharton:butterknife-compiler:8.4.0'

    你还需要添加

    类路径'com.neenbedankt.gradle.plugins:android-apt:1.8'

    到您的项目级 build.gradle 文件 -- 请参阅 https://github.com/JakeWharton/butterknife#download 了解更多信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多