John M.
John M.

Reputation: 2712

Is getActionBar() supposed to return null?

I'm trying to make a Toolbar in a fragment an Action Bar. This is what I do:

    View view = inflater.inflate(R.layout.fragment_sourceitem_list, viewGroup, false);

    android.support.v7.widget.Toolbar toolbar = (android.support.v7.widget.Toolbar) view.findViewById(R.id.toolbar);
    ((AppCompatActivity) mListener).setSupportActionBar(toolbar);

where mListener is the Activity that contains the fragment.

However, if I have the following straight after

    ActionBar actionbar = ((AppCompatActivity) mListener).getActionBar();

actionbar is null. How come it's still null even when the ActionBar has been set in the previous line already? Otherwise, what's a good way to set the property of the newly set ActionBar?

Thanks

Upvotes: 3

Views: 1283

Answers (1)

Niki van Stein
Niki van Stein

Reputation: 10724

Since you use AppCompatActivity you use the supportActionBar and therefor you need to use getSupportActionBar().

This supports older android versions than the normal Activity and ActionBar.

Upvotes: 4

Related Questions