getSupportActionBar(toolbar) throws null pointer exception using with Butterknife for toolbar
Asked Answered
D

2

6

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'
Dessalines answered 23/7, 2016 at 16:3 Comment(15)
Why not just findViewById for your Toolbar>Kowalski
I'm sure it's not related to Jellybean.Saltandpepper
can you put some logs?Saltandpepper
at android.support.v7.app.AppCompatActivity.setSupportActionBar(AppCompatActivity.java:130) at example.materialtoolbarexample.MainActivity.onCreate(MainActivity.java:24)Dessalines
@Kowalski I am using butterknife so, if there any better way to get the toolbar .Dessalines
Hello @SatanPandeya , have you initialized toolbar . i suppose you forgot to add toolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);Polash
please share your activity layout xml fileKowalski
@ShresthaRohit At MainActivity: @BindView(R.id.tool_bar_demo) Toolbar toolbar;Dessalines
@Kowalski Plz find the xml layout file in my question. activity_main is also included.Dessalines
@Saltandpepper java.lang.RuntimeException: Unable to start activity ComponentInfo{example.materialtoolbarexample/example.materialtoolbarexample.MainActivity}: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.Dessalines
@SatanPandeya I think it's a problem with your style. you should use NoActionBar style.Saltandpepper
<style name="YOUR_THEME" parent="Theme.AppCompat.Light.NoActionBar">Saltandpepper
@Saltandpepper This time the code work and run without crashing but still throws java.lang.RuntimeException: Unable to start activity ComponentInfo{example.materialtoolbarexample/example.materialtoolbarexample.MainActivity}: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead. Thanks for your advice and time . Is there is any way to avoid this ?Dessalines
@SatanPandeya Can you post your Style ?Saltandpepper
is a problem solved?Nitrite
B
1

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

Blasphemous answered 23/7, 2016 at 16:39 Comment(0)
S
1

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.

Saltandpepper answered 23/7, 2016 at 17:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.