NullPointerException with android.support.v7.widget.Toolbar

后端 未结 12 885
旧巷少年郎
旧巷少年郎 2021-01-07 17:53

I am currently trying to implement a navigation drawer in my app. I am having an issue with the Toolbar being null. I have done many searches online and tried many different

相关标签:
12条回答
  • 2021-01-07 18:19

    I solved the issue with the toolbar. I realized after hours of searching the web and trying different things, I realized that my code wasn't wrong, it was using layouts from the layout-21 not the layout folder since the emulation for Android 5.0. All modification I had made were in the layout folder. I made the changes to the xml files in the layout-21 folder and the problem was solved. Sorry for the wasted time and post.

    0 讨论(0)
  • 2021-01-07 18:21

    I read another time your code...and I saw that you haven't defined the ID of your toolbar in your MainActivity XML...see your include in it :

    <include layout="@layout/app_bar" />
    
    <!-- But where's ID attribute here ? -->
    
    <!-- add android:id="@+id/app_bar" -->
    
    <!-- to finally have below : -->
    
    <include layout="@layout/app_bar" android:id="@+id/app_bar" />
    

    After that...you won't get this error 'cause your toolbar is finally found and isn't set to 'null'.

    0 讨论(0)
  • 2021-01-07 18:23

    Answer is a little late but I just ran into this problem.

    I had a similar issue and spent 7 hrs messing with it. It worked on 3 of 4 activities just fine but one would crash.

    The only thing that worked for me, and I am not sure why, was to put the:

        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(mToolbar);
        getSupportActionBar().setDisplayShowHomeEnabled(true); 
    

    as the last code in my onCreate. It works perfectly now.

    0 讨论(0)
  • 2021-01-07 18:24

    I had an issue with Toolbar that wasn't of support.v7 I mean that you must add android.support.v7.widget. before the cast you realize when finding Toolbar with ID...

    0 讨论(0)
  • 2021-01-07 18:31

    In my own case, i had used

    @Bind(R.id.toolbar)
    protected Toolbar toolbar;
    

    at the top of my code but forgot to do

    ButterKnife.bind(this);
    

    inside onCreate() method

    I hope that helps someone

    Remember to add

     compile 'com.jakewharton:butterknife:7.0.1'
    

    to your build.gradle dependencies and import

    import butterknife.Bind;
    import butterknife.ButterKnife;
    

    in your java code and rebuild your app.

    0 讨论(0)
  • 2021-01-07 18:31

    check both line call is ok or not

        setContentView(R.layout.layout_file)
        toolbar = (Toolbar) findViewById(R.id.toolbar_top)
    
    0 讨论(0)
提交回复
热议问题