微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

未能在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.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐