java.lang.IllegalStateException:即使在 setContentView() 之后也找不到所需的视图
java.lang.IllegalStateException: Required view not found even after setContentView()
我遇到了一个与未找到视图相关的奇怪问题,但代码似乎一切正常(至少对我而言)。事实上,该程序在一段时间前运行良好。我不确定我做了什么改变导致了这个问题。你们中的任何人都可以指出我的问题吗?谢谢。
下面是异常轨迹:
FATAL EXCEPTION: main
Process: co.mycompany, PID: 3923
java.lang.RuntimeException: Unable to start activity ComponentInfo{co.mycompany/co.mycompany.activities.MainActivity}: java.lang.RuntimeException: Unable to bind views for co.mycompany.activities.MainActivity
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2201)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2250)
at android.app.ActivityThread.access0(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1213)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5065)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.RuntimeException: Unable to bind views for co.mycompany.activities.MainActivity
at butterknife.ButterKnife.bind(ButterKnife.java:322)
at butterknife.ButterKnife.bind(ButterKnife.java:237)
at co.mycompany.activities.MainActivity.onCreate(MainActivity.java:51)
at android.app.Activity.performCreate(Activity.java:5249)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2165)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2250)
at android.app.ActivityThread.access0(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1213)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5065)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalStateException: Required view 'thUserProfilePic' with ID 2131558656 for field 'mUserProfilePic' was not found. If this view is optional add '@Nullable' annotation.
at butterknife.ButterKnife$Finder.findRequiredView(ButterKnife.java:140)
at co.mycompany.activities.MainActivity$$ViewBinder.bind(MainActivity$$ViewBinder.java:17)
at co.mycompany.activities.MainActivity$$ViewBinder.bind(MainActivity$$ViewBinder.java:8)
at butterknife.ButterKnife.bind(ButterKnife.java:319)
at butterknife.ButterKnife.bind(ButterKnife.java:237)
at co.mycompany.activities.MainActivity.onCreate(MainActivity.java:51)
at android.app.Activity.performCreate(Activity.java:5249)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2165)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2250)
at android.app.ActivityThread.access0(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1213)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5065)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
at dalvik.system.NativeStart.main(Native Method)
MainActivity.class
@Bind(R.id.thUserProfilePic)
CircleImageView mUserProfilePic;
@Bind(R.id.thUserDisplayName)
TextView mUserDisplayName;
@Bind(R.id.editProfileLink)
ImageView editProfileLink;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
setSupportActionBar(mToolbar);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, mDrawerLayout, mToolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
mDrawerLayout.setDrawerListener(toggle);
toggle.syncState();
mNavigationView.setNavigationItemSelectedListener(this);
navigate(AppScreen.HOME);
}
R.layout.activity_main
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" android:id="@+id/drawer_layout"
android:layout_width="match_parent" android:layout_height="match_parent"
android:fitsSystemWindows="true" tools:openDrawer="start">
<include layout="@layout/app_bar_main" android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView android:id="@+id/nav_view"
android:layout_width="wrap_content" android:layout_height="match_parent"
android:layout_gravity="start" android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main" app:menu="@menu/activity_main_drawer"
android:background="@color/white"/>
</android.support.v4.widget.DrawerLayout>
R.layout.nav_header_main
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="@dimen/nav_header_height"
android:background="@drawable/side_nav_bar"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:theme="@style/ThemeOverlay.AppCompat.Dark" android:orientation="vertical"
android:gravity="center">
<ImageView
android:id="@+id/editProfileLink"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_edit_white_24dp"
android:layout_gravity="end|right"/>
<include layout="@layout/user_thumbnail_large"/>
</LinearLayout>
R.layout.user_thumnail_large
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center_horizontal"
android:id="@+id/userThumbnailLargeContainer">
<de.hdodenhof.circleimageview.CircleImageView android:layout_width="75dp" android:layout_height="75dp"
android:paddingTop="@dimen/nav_header_vertical_spacing"
android:src="@drawable/ic_face_white_48dp" android:id="@+id/thUserProfilePic" />
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:paddingTop="@dimen/nav_header_vertical_spacing" android:text="John"
android:textAppearance="@style/TextAppearance.AppCompat.Body1"
android:id="@+id/thUserDisplayName"
android:textColor="@color/textColorSecondary"/>
</LinearLayout>
我进行了第二天的研究,无法弄清楚问题是什么。
问题与支持库版本有关。我将支持库升级到 23 版本后出现上述错误。
下面link有解决方法
我遇到了一个与未找到视图相关的奇怪问题,但代码似乎一切正常(至少对我而言)。事实上,该程序在一段时间前运行良好。我不确定我做了什么改变导致了这个问题。你们中的任何人都可以指出我的问题吗?谢谢。
下面是异常轨迹:
FATAL EXCEPTION: main
Process: co.mycompany, PID: 3923
java.lang.RuntimeException: Unable to start activity ComponentInfo{co.mycompany/co.mycompany.activities.MainActivity}: java.lang.RuntimeException: Unable to bind views for co.mycompany.activities.MainActivity
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2201)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2250)
at android.app.ActivityThread.access0(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1213)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5065)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.RuntimeException: Unable to bind views for co.mycompany.activities.MainActivity
at butterknife.ButterKnife.bind(ButterKnife.java:322)
at butterknife.ButterKnife.bind(ButterKnife.java:237)
at co.mycompany.activities.MainActivity.onCreate(MainActivity.java:51)
at android.app.Activity.performCreate(Activity.java:5249)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2165)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2250)
at android.app.ActivityThread.access0(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1213)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5065)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalStateException: Required view 'thUserProfilePic' with ID 2131558656 for field 'mUserProfilePic' was not found. If this view is optional add '@Nullable' annotation.
at butterknife.ButterKnife$Finder.findRequiredView(ButterKnife.java:140)
at co.mycompany.activities.MainActivity$$ViewBinder.bind(MainActivity$$ViewBinder.java:17)
at co.mycompany.activities.MainActivity$$ViewBinder.bind(MainActivity$$ViewBinder.java:8)
at butterknife.ButterKnife.bind(ButterKnife.java:319)
at butterknife.ButterKnife.bind(ButterKnife.java:237)
at co.mycompany.activities.MainActivity.onCreate(MainActivity.java:51)
at android.app.Activity.performCreate(Activity.java:5249)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2165)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2250)
at android.app.ActivityThread.access0(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1213)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5065)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
at dalvik.system.NativeStart.main(Native Method)
MainActivity.class
@Bind(R.id.thUserProfilePic)
CircleImageView mUserProfilePic;
@Bind(R.id.thUserDisplayName)
TextView mUserDisplayName;
@Bind(R.id.editProfileLink)
ImageView editProfileLink;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
setSupportActionBar(mToolbar);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, mDrawerLayout, mToolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
mDrawerLayout.setDrawerListener(toggle);
toggle.syncState();
mNavigationView.setNavigationItemSelectedListener(this);
navigate(AppScreen.HOME);
}
R.layout.activity_main
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" android:id="@+id/drawer_layout"
android:layout_width="match_parent" android:layout_height="match_parent"
android:fitsSystemWindows="true" tools:openDrawer="start">
<include layout="@layout/app_bar_main" android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView android:id="@+id/nav_view"
android:layout_width="wrap_content" android:layout_height="match_parent"
android:layout_gravity="start" android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main" app:menu="@menu/activity_main_drawer"
android:background="@color/white"/>
</android.support.v4.widget.DrawerLayout>
R.layout.nav_header_main
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="@dimen/nav_header_height"
android:background="@drawable/side_nav_bar"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:theme="@style/ThemeOverlay.AppCompat.Dark" android:orientation="vertical"
android:gravity="center">
<ImageView
android:id="@+id/editProfileLink"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_edit_white_24dp"
android:layout_gravity="end|right"/>
<include layout="@layout/user_thumbnail_large"/>
</LinearLayout>
R.layout.user_thumnail_large
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center_horizontal"
android:id="@+id/userThumbnailLargeContainer">
<de.hdodenhof.circleimageview.CircleImageView android:layout_width="75dp" android:layout_height="75dp"
android:paddingTop="@dimen/nav_header_vertical_spacing"
android:src="@drawable/ic_face_white_48dp" android:id="@+id/thUserProfilePic" />
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:paddingTop="@dimen/nav_header_vertical_spacing" android:text="John"
android:textAppearance="@style/TextAppearance.AppCompat.Body1"
android:id="@+id/thUserDisplayName"
android:textColor="@color/textColorSecondary"/>
</LinearLayout>
我进行了第二天的研究,无法弄清楚问题是什么。
问题与支持库版本有关。我将支持库升级到 23 版本后出现上述错误。
下面link有解决方法