0

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

Al Hennessey
  • 2,395
  • 8
  • 39
  • 63

3 Answers3

3

I think the problem is that you access the toolbar before view inflation which results in NullPointerException. And also since you are using support library you need to cast your toolbar to android.support.v7.widget.Toolbar. Here is a solution which may help:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        android.support.v7.widget.Toolbar toolbar = (android.support.v7.widget.Toolbar) rootView.findViewById(R.id.toolbar);
        ((AppCompatActivity)getActivity()).setSupportActionBar(toolbar);
        ((AppCompatActivity)getActivity()).getSupportActionBar().setDisplayShowHomeEnabled(true);    

        return rootView;
}
Pooya
  • 6,083
  • 3
  • 23
  • 43
  • if he has `import android.support.v7.widget.Toolbar;` then casting `(Toolbar)` is the same as `(android.support.v7.widget.Toolbar)` ... but yeah you are right – Selvin Mar 17 '16 at 09:36
  • Thanks for your answer i was calling it after view inflation, however, i get error `cannot resolve method` for `setHomeAsUpEnabled`, can i not do this in the fragment? – Al Hennessey Mar 17 '16 at 09:40
  • @AlHennessey I updated my code and tested it in Android Studio but didn't get any error. Please try the edit – Pooya Mar 17 '16 at 09:56
  • Hi, thanks for the help, i updated my code but i get a null pointer exception on the `displayShowHomeEnabled` line. However if i leave the `android.support.v7.widget.Toolbar toolbar = (android.support.v7.widget.Toolbar) rootView.findViewById(R.id.toolbar);` and the `setSupportActionBar` line out, the back button appears, however it doesnt do anything when pressed – Al Hennessey Mar 17 '16 at 10:06
  • Solved it by setting the `SetDisplayHomeAsUpEnabled` in the activity on create and then just using lines like `((AppCompatActivity) getActivity()).getSupportActionBar().setTitle("Login");` to chang it in activity. To solve my problem of the button not doing anything i added `OnBackPressed` line to `OnOptionsItemSelected` Thanks for your help tho – Al Hennessey Mar 17 '16 at 10:22
  • @AlHennessey if you want the Up button to do something you need to define in your manifest what the parent of your activity is. then if you press the button it will go back to the parent, otherwise it doesn't do anything – Pooya Mar 17 '16 at 10:23
  • @Pooya i was trying to do this with fragments not activities. – Al Hennessey Mar 17 '16 at 10:24
  • @AlHennessey toolbars are actually for activities that's why you need to set the toolbar by calling getActivity().setSupportActionBar – Pooya Mar 17 '16 at 10:25
-1

((Activity)getActivity()).setSupportActionBar(toolbar);

zys
  • 1,306
  • 3
  • 18
  • 37
-1

Modify this:

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

to this:

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

Hope it helps!!!

BRG
  • 380
  • 6
  • 24