Butterknife: Exception when bind views in BaseActivity

Created on 28 Aug 2015  路  11Comments  路  Source: JakeWharton/butterknife

Hello.

I have BaseActivity:

public class BaseActivity extends AppCompatActivity {
@Bind(R.id.frame_container)
FrameLayout mFrameLayout;

@Bind(R.id.next_button)
Button mNextButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    super.setContentView(R.layout.activity_base);

    ButterKnife.bind(this);
}

@Override
public void setContentView(@LayoutRes int layoutResID) {
    getLayoutInflater().inflate(layoutResID, mFrameLayout, true);
}

}

and MainActivity:

public class MainActivity extends BaseActivity {

@Bind(R.id.text1)
TextView mText;

@Bind(R.id.edit)
EditText mEdit;

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

}

On start application ButterKnife throw exception:

java.lang.RuntimeException: Unable to start activity ComponentInfo{ro.bla/ro.bla.MainActivity}: java.lang.RuntimeException: Unable to bind views for ro.bla.MainActivity
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.RuntimeException: Unable to bind views for ro.bla.MainActivity
at butterknife.ButterKnife.bind(ButterKnife.java:322)
at butterknife.ButterKnife.bind(ButterKnife.java:237)
at ro.bla.BaseActivity.onCreate(BaseActivity.java:27)
at ro.bla.MainActivity.onCreate(MainActivity.java:20)
at android.app.Activity.performCreate(Activity.java:5133)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
聽聽聽聽聽聽聽聽聽聽聽聽at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
聽聽聽聽聽聽聽聽聽聽聽聽at android.app.ActivityThread.access$600(ActivityThread.java:141)
聽聽聽聽聽聽聽聽聽聽聽聽at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
聽聽聽聽聽聽聽聽聽聽聽聽at android.os.Handler.dispatchMessage(Handler.java:99)
聽聽聽聽聽聽聽聽聽聽聽聽at android.os.Looper.loop(Looper.java:137)
聽聽聽聽聽聽聽聽聽聽聽聽at android.app.ActivityThread.main(ActivityThread.java:5103)
聽聽聽聽聽聽聽聽聽聽聽聽at java.lang.reflect.Method.invokeNative(Native Method)
聽聽聽聽聽聽聽聽聽聽聽聽at java.lang.reflect.Method.invoke(Method.java:525)
聽聽聽聽聽聽聽聽聽聽聽聽at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
聽聽聽聽聽聽聽聽聽聽聽聽at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
聽聽聽聽聽聽聽聽聽聽聽聽at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalStateException: Required view 'text1' with ID 2131492943 for field 'mText' was not found. If this view is optional add '@Nullable' annotation.
at butterknife.ButterKnife$Finder.findRequiredView(ButterKnife.java:140)
at ro.bla.MainActivity$$ViewBinder.bind(MainActivity$$ViewBinder.java:12)
at ro.bla.MainActivity$$ViewBinder.bind(MainActivity$$ViewBinder.java:7)
at butterknife.ButterKnife.bind(ButterKnife.java:319)
聽聽聽聽聽聽聽聽聽聽聽聽at butterknife.ButterKnife.bind(ButterKnife.java:237)
聽聽聽聽聽聽聽聽聽聽聽聽at ro.bla.BaseActivity.onCreate(BaseActivity.java:27)
聽聽聽聽聽聽聽聽聽聽聽聽at ro.bla.MainActivity.onCreate(MainActivity.java:20)
聽聽聽聽聽聽聽聽聽聽聽聽at android.app.Activity.performCreate(Activity.java:5133)
聽聽聽聽聽聽聽聽聽聽聽聽at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
聽聽聽聽聽聽聽聽聽聽聽聽at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
聽聽聽聽聽聽聽聽聽聽聽聽at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
聽聽聽聽聽聽聽聽聽聽聽聽at android.app.ActivityThread.access$600(ActivityThread.java:141)
聽聽聽聽聽聽聽聽聽聽聽聽at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
聽聽聽聽聽聽聽聽聽聽聽聽at android.os.Handler.dispatchMessage(Handler.java:99)
聽聽聽聽聽聽聽聽聽聽聽聽at android.os.Looper.loop(Looper.java:137)
聽聽聽聽聽聽聽聽聽聽聽聽at android.app.ActivityThread.main(ActivityThread.java:5103)
聽聽聽聽聽聽聽聽聽聽聽聽at java.lang.reflect.Method.invokeNative(Native Method)
聽聽聽聽聽聽聽聽聽聽聽聽at java.lang.reflect.Method.invoke(Method.java:525)
聽聽聽聽聽聽聽聽聽聽聽聽at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
聽聽聽聽聽聽聽聽聽聽聽聽at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
聽聽聽聽聽聽聽聽聽聽聽聽at dalvik.system.NativeStart.main(Native Method)

How I can fix it and that I to do?
Thanks.

Most helpful comment

The message in the exception tells you what to do:

Required view 'text1' with ID 2131492943 for field 'mText' was not found. If this view is optional add '@Nullable' annotation.

Either add it to your layout or make the field binding optional with a @Nullable annotation.

All 11 comments

The message in the exception tells you what to do:

Required view 'text1' with ID 2131492943 for field 'mText' was not found. If this view is optional add '@Nullable' annotation.

Either add it to your layout or make the field binding optional with a @Nullable annotation.

So. I must annotate all views in sub activity as @Nullable?

Only if they're optional. If they're not optional then it is missing from your layout.

Ok. Thanks.

Same error here:
@Nullable
@OnClick({R.id.detail_page_tv_button, R.id.play_on_tv_btn})
protected abstract void onTvButtonClicked();

Required view 'play_on_tv_btn' with ID 2131820986 for method 'onTvButtonClicked' was not found
How to avoid it?

Did this fix the problem?

What did not work for me:

@Nullable
@OnClick({R.id.detail_page_tv_button, R.id.play_on_tv_btn})

What WORKED for me:

@Optional
@OnClick({R.id.detail_page_tv_button, R.id.play_on_tv_btn})

@JakeWharton This issue is happening when the view's Visibility is GONE in the layout. why is that ?

In my case this error was because I was using two layouts for different versions:

  • activity_login.xml

  • activity_login.xml (v21)

I added a progressBar in activity_login.xml but I forgot to added to activity_login.xml (v21). A really stupid mistake 馃檲

In my case, I use a nav drawer with a header layout. It returns the same exception. The view exists but it's gone when the drawer is not opened !

set @Nullable for problem field helped for me

Was this page helpful?
0 / 5 - 0 ratings