我使用butterknife在android studio中开发我的应用程序,并在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);
}
}