未能在Android Studio和Android 5.0中使用ButterKnife

用户使用butterknife来开发我的 Android工程师APP并在android 4.4中测试它表现不错.

但是当我在Android 5.0上面的手机中测试时,它总是会抛出如下错误.

Caused by: java.lang.RuntimeException: Unable to bind views for com.quinn.githubknife.ui.activity.MainActivity
        at butterknife.ButterKnife.bind(ButterKnife.java:322)
        at butterknife.ButterKnife.bind(ButterKnife.java:237)
        at com.quinn.githubknife.ui.activity.MainActivity.onCreate(MainActivity.java:70)
        at android.app.Activity.performCreate(Activity.java:5990)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
        at android.app.ActivityThread.performlaunchActivity(ActivityThread.java:2278)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
        at android.app.ActivityThread.access$800(ActivityThread.java:151)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5257)

 Caused by: ava.lang.IllegalStateException: required view 'toolbar' with ID 2131689575 for field 'toolbar' was not found. If this view is optional add '@Nullable' annotation.
            at butterknife.ButterKnife$Finder.findrequiredView(ButterKnife.java:140)
            at com.quinn.githubknife.ui.activity.MainActivity$$ViewBinder.bind(MainActivity$$ViewBinder.java:11)
            at com.quinn.githubknife.ui.activity.MainActivity$$ViewBinder.bind(MainActivity$$ViewBinder.java:8)
            at butterknife.ButterKnife.bind(ButterKnife.java:319)
            at butterknife.ButterKnife.bind(ButterKnife.java:237)
            at com.quinn.githubknife.ui.activity.MainActivity.onCreate(MainActivity.java:70)
            at android.app.Activity.performCreate(Activity.java:5990)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
            at android.app.ActivityThread.performlaunchActivity(ActivityThread.java:2278)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
            at android.app.ActivityThread.access$800(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5257)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

我的核心java代码是:

public class MainActivity extends BaseActivity implements MainAuthView,NavigationView.OnNavigationItemSelectedListener {

    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.drawer_layout)
    DrawerLayout mDrawerLayout;
    @Bind(R.id.nav_view)
    NavigationView navigationVIew;
    @Bind(R.id.viewpager)
    ViewPager viewpager;
    @Bind(R.id.tabs)
    TabLayout tab;


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

解决方法

ButterKnife无法为您带注释的工具栏视图找到R.id.toolbar并抛出一个摇摆不定的东西.视图的id是错误的,或者在R.layout.activity_main中不存在.如果你认为它是可选的,那么你可以添加@Nullable.

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...