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
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.
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
'.
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.
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...
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.
check both line call is ok or not
setContentView(R.layout.layout_file)
toolbar = (Toolbar) findViewById(R.id.toolbar_top)