Butterknife: Binded views are null

Created on 7 Jul 2015  ·  3Comments  ·  Source: JakeWharton/butterknife

We migrated from 6 to 7.0.1, replaced @InjectView with @Bind and ButterKnife.inject(this) with ButterKnife.bind(this) and now the app crashes with NullPointerException when I call method on any binded view.

import butterknife.Bind;
import butterknife.ButterKnife;

public class ActivityMain extends AppCompatActivity {
    @Bind(R.id.progress)
    ProgressBar progress;
    @Bind(R.id.frame)
    FrameLayout layout;

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_landing);
        ButterKnife.bind(this);
        Assert.assertNotNull(layout);   //Crashes!
        Assert.assertNotNull(progress); //Crashes!
    }

    @Override
    protected void attachBaseContext(final Context newBase) {
        super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));   // https://github.com/chrisjenx/Calligraphy
    }
}

The above code works with 6, so the layout and ids are ok.

Most helpful comment

ProGuard!

Change

-keep class **$$ViewInjector { *; }

to

-keep class **$$ViewBinder { *; }

All 3 comments

Can you add ButterKnife.setDebug(true); above your bind command and post a log please?

If you are using ProGuard have you updated your configuration? I was hit by this last week. https://github.com/JakeWharton/butterknife/issues/285

-  -keep class **$$ViewInjector { *; }
+  -keep class **$$ViewBinder { *; }
07-07 10:32:52.273  23435-23435/? D/ButterKnife﹕ Looking up view binder for cz.master.anna.ui.landingpage.ActivityMain
07-07 10:32:52.273  23435-23435/? D/ButterKnife﹕ Not found. Trying superclass cz.master.anna.ui.BaseActivity
07-07 10:32:52.274  23435-23435/? D/ButterKnife﹕ Not found. Trying superclass android.support.v7.app.AppCompatActivity
07-07 10:32:52.274  23435-23435/? D/ButterKnife﹕ MISS: Reached framework class. Abandoning search.

Actually, ActivityMain extends BaseActivity (that's where attachBaseContext is, because we want to use Calligraphy everywhere) and BaseActivity extends AppCompatActivity.

So maybe we do something wrong in BaseActivity? But every overriden method here is calling super, so I guess we don't.

ProGuard!

Change

-keep class **$$ViewInjector { *; }

to

-keep class **$$ViewBinder { *; }
Was this page helpful?
0 / 5 - 0 ratings