I'm using a Toolbar to replace the ActionBar. All is going well with one problem:
The toolbar shows only on the main activity.
If I try call the toolbar on any activity the same way I did with the main activity the app will crash when I call that activity.
If I try to inflate the toolbar onCreateOptionsMenu, that activity will crash when I call it.
How can I call/use the same toolbar across all my activities and not just the main one.
here is some pieces of the code:
public android.support.v7.widget.Toolbar toolbar;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_about);
toolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.app_bar_id);
setSupportActionBar(toolbar);
}
The code above works to call the toolbar successfully, but it only works if i use it on the main activity, the rest of the activities will crash if I called the toolbar there the same method shown above.
Some help please?
Thank you.
Edited:
Upon Request here are more code fragments:
app_bar.xml :
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/actionbarbgcolor"
app:popupTheme="@style/popUpTheme">
</android.support.v7.widget.Toolbar>
themes.xml (styles.xml replacement):
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools">
<style name="DefaultActionBarTheme" parent="Theme.AppCompat.NoActionBar">
<item name="android:windowNoTitle">true</item>
<item name="android:windowActionBar">false</item>
<item name="android:textColorPrimary">@color/windowbackgroundcolor</item>
<item name="android:windowBackground">@color/windowbackgroundcolor</item>
</style>
<style name="popUpTheme">
<item name="android:textColor">@color/actionbarbgcolor</item>
</style>
</resources>
I found the solution, I forgot to include the toolbar in the rest of the activities layout files. So I was calling a toolbar that didn't exist in that activity's layout.
I only had it included in the main activity so that's why it worked there and crashed at the rest.
For beginners, this mean the following code must exist in every layout xml file you wish the toolbar to work in:
<include layout="@layout/app_bar"/>
Note that "app_bar" is just the name I called my toolbar with, yours can be different.
Good luck.