I have added a toolbar in my activity and am trying to access it in my fragment to change the title and icon for the back navigation, however i keep getting the error
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)' on a null object reference
When i run my app as well as a number of warnings about possible object being null. I know it has something to do with getsupportactionbar
however i cannot work out what i am doing wrong
Here is the code for declaring the toolbar in activity
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
The code for referencing and changing the toolbar in fragment
Toolbar toolbar = (Toolbar) getActivity().findViewById(R.id.toolbar);
((AppCompatActivity)getActivity()).setSupportActionBar(toolbar);
((AppCompatActivity)getActivity()).getSupportActionBar().setDisplayShowHomeEnabled(true);
And the code for declaring the toolbar in xml
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorAccent"
android:layout_alignParentTop="true"
app:popupTheme="@style/AppTheme.PopupOverlay" />
I am using appcompatactivity
if it helps