I am using Butterknife to inject view for the toolbar. But the method getSupportActionBar(toolbar)
throws null pointer exception and the app unfortunately stopped. What can be done to solve this issue? I am using Android 4.2. So, is there any problem of using Butterknife with Jellybean?
MainActivity
public class MainActivity extends AppCompatActivity {
@BindView(R.id.tool_bar_demo)
Toolbar toolbar;
@BindDrawable(R.drawable.backspace)
Drawable backspace_btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
setSupportActionBar(toolbar);
toolbar.setTitle("ABC");
}
}
activity_main
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="58dp"
android:id="@+id/tool_bar_demo"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:titleTextColor="@color/colorAccent"
/>
</RelativeLayout>
gradle
minSdkVersion 15
targetSdkVersion 23
apply plugin: 'com.neenbedankt.android-apt'
compile 'com.android.support:design:23.4.0'
compile 'com.jakewharton:butterknife:8.0.1'
apt 'com.jakewharton:butterknife-compiler:8.0.1'
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
This seems to be a issue with Butterknife.
From the issue the main points collected are:
Butter Knife is just sugar on findViewById, and that's what is failing to find the view. As I said, make sure that layout is in main_activity or ensure you put a Toolbar with that ID in main_activity layout.
another comment says: if i set an id to my include like this :
<include
android:layout_width="match_parent"
android:layout_height="wrap_content"
layout="@layout/toolbar"
android:id="@+id/include4" />
That doesn't work. But if i remove the android:id="@+id/include4"
it works. As of version 7.0.1, adding an ID to the include causes the view to not be found.
The link to the issue: id issue of butterknife
In fact it's not related to Butterknife
or Jellybean
. It's common exception that happen when you use ActionBar theme with Toolbar
. And it's says you can't use ActionBar
& Toolbar
at the same time.
As I mention in comment you should change your style to following:
<style name="YOUR_STYLE" parent="Theme.AppCompat.Light.NoActionBar">
.... other attrs here
<item name="windowActionBar">false</item>
</style>
See this related thread to solve your issue.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With