.getSupportActionBar not available in Fragment; workaround leads to NullPointerException on rotation

getsupportactionbar cannot resolve fragmentactivity
getsupportactionbar not resolved
toolbar in fragment
setsupportactionbar in fragment
how to access actionbar from fragment
fragment toolbar is null
how to add toolbar in fragmentactivity in android
change toolbar in fragment

Structure

(names of Activies/Fragments changed)

Based on some advice written in Android Programming: The Big Nerd Ranch Guide, I have the following structure:

  • MyListFragment extends ListFragment
  • MyListActivity extends SimpleFragmentActivity
  • SimpleFragmentActivity extends ActionBarActivity

MyListActivity.java

public class MyListActivity extends SingleFragmentActivity {

    @Override
    protected Fragment createFragment() {
        return new MyListFragment();
    }
}

SingleFragmentActivity.java

public abstract class SingleFragmentActivity extends ActionBarActivity {

    protected abstract Fragment createFragment ();

    @Override
    public void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fragment);

        FragmentManager fm = getSupportFragmentManager();
        Fragment fragment = fm.findFragmentById(R.id.fragmentContainer);

        if (fragment == null) {
            fragment = createFragment();
            fm.beginTransaction().add(R.id.fragmentContainer, fragment).commit();
        }
    }
}

After having outlined and programmed the basic functionality of my app (including Contextual Action Bar, navigation, …) I wanted to make sure that the app is as backwards compatible as possible without too much additional effort. Therefore, I started using the AppCompat (com.android.support:appcompat-v7:20+) ActionBar.

What baffled me right at the beginning was that—opposed to every tutorial or Android’s Developer guidelines I read—the getSupportActionBar method has never been available in Android Studio (I’m using Android Studio v0.8.2 and JRE 1.7_60). Whenever I type getActivity().getSup, the only suggestions I get are getSupportFragmentManager and getSupportLoaderManager.

As a workaround, I create a member variable ActionBar mActionBar in MyListFragment and initialise it:

@Override
public void onAttach (Activity activity) {
    super.onAttach(activity);
    mActionBar = ((ActionBarActivity) activity).getSupportActionBar();
}

That seemed to do the trick on either Android 4.4.4 (Moto G) and 2.3.4 (LG E-510)—the only devices I have available.

However—coming to the problem I have—on rotation the ActionBar assignment gets lost and the application crashes with a NullPointerException. Debugging the application and setting a breakpoint on the following line in onCreate:

mActionBar.setTitle(R.string.title_activity_my_list);

I found out that mActionBar is null after the rotation. Placing said line in onCreateView didn’t help.

What I tried

  • Placing the initialisation of mActionBar in
    • onAttach
    • onActivityCreated
    • onCreate/onCreateView right before .setTitle gets called

I’m out of ideas here and grateful for advice.

EDIT-0 (30.07.)

Exception being thrown (after editing according to Alex’ suggestion)

07-30 15:05:25.239    5049-5049/MYAPPNAME E/AndroidRuntime﹕ [Blue Error Handler] Make Debugging Report file for main
java.lang.RuntimeException: Unable to start activity ComponentInfo{MYAPPNAME/MYAPPNAME.MYLISTACTIVITY}: java.lang.NullPointerException
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1653)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1669)
    at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:2838)
    at android.app.ActivityThread.access$1600(ActivityThread.java:117)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:130)
    at android.app.ActivityThread.main(ActivityThread.java:3737)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:507)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:894)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:652)
    at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
    at MYAPPNAME.MYLISTFRAGMENT.onCreate(MYLISTFRAGMENT.java:54)
    at android.support.v4.app.Fragment.performCreate(Fragment.java:1481)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:908)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1121)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1103)
    at android.support.v4.app.FragmentManagerImpl.dispatchCreate(FragmentManager.java:1896)
    at android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:216)
    at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:98)
    at MYAPPNAME.SingleFragmentActivity.onCreate(SingleFragmentActivity.java:22)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1617)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1669)
    at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:2838)
    at android.app.ActivityThread.access$1600(ActivityThread.java:117)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:130)
    at android.app.ActivityThread.main(ActivityThread.java:3737)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:507)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:894)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:652)
    at dalvik.system.NativeStart.main(Native Method)

MyListFragment line 54 (exception occured using either SingleFragmentActivity and ActionBarActivity as a cast)

((SingleFragmentActivity) getActivity()).getSupportActionBar().setTitle(R.string
            .title_activity_my_list);

SingleFragmentActivity lines 20-22:

@Override
public void onCreate (Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

Cast your getActivity() to ActionBarActivity/AppCompatActivity (depends what are you using) and you will have access to the support ActionBar.

((AppCompatActivity)getActivity()).getSupportActionBar();

or

((ActionBarActivity)getActivity()).getSupportActionBar();

Use this code in onActivityCreated(...) method instead of onAttach(...)

android – Toolbar – add the up button – in Android, setDisplayHomeAsUpEnabled(true); Or in case of using in Fragment: Toolbar toolbar getSupportActionBar not available in Fragment; workaround leads to NullPointerException on rotation - in AndroidDans "Non classé". Something like on the first call to getSupportActionBar() if setContentView() was never called and a fragment transaction was committed to be attached to android.R.id.content then automatically call setContentView with new FrameLayout(this).

Now it should actually be:

((AppCompatActivity)getActivity()).setSupportActionBar();

because ActionBarActivity is deprecated.

But this is only if you must do this outside of an activity, where it's most commonly done.

Action Bar not showing in the Fragment, NullPointerException: Attempt to invoke virtual method 'void android.support.v7.​app.ActionBar.setHomeButtonEnabled(boolean)' on a null  Of course, it won't work. You have to setTitle using CollapsingToolbarLayout. It's from Google Support Design. This setup uses CollapsingToolbarLayout’s app:layout_collapseMode="pin" to ensure that the Toolbar itself remains pinned to the top of the screen while the view collapses.

Even though this answers is given very late it can help someone. We can directly call like following code ((AppCompatActivity)getActivity()).getSupportActionBar().setTitle("String"); It also works fine.

Android Sliding Menu using Navigation Drawer, In our navigation drawer menu, there are two other menu items, About Ravi, check that this 2MB restriction is not due to PHP's configuration (ie. php.ini). android:minSdkVersion=”8″ , but it still force close . how to fix it ? When the device is rotated the fragment title goes back to the Application name. The following are top voted examples for showing how to use android.support.v7.app.ActionBarActivity.These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.

Sometime it can be null value. So just add like this example.

Objects.requireNonNull(((AppCompatActivity) Objects.requireNonNull(getActivity())).getSupportActionBar()).hide();

NullPointerException in Fragments onAttach for findViewById, NullPointerException in Fragments onAttach for findViewById - android. So I have a curious problem I'm not able to solve right now. Is the Activity removed from the stack, which leads to the Exception? fix that, I put my code in onActivityCreated, I guess I will only put this there for now getSupportActionBar​().show(); }  The about entry is not needed in the fragment, because it is added by the activity, no matter whether you are in single-pane or double-pane mode. Changing the order of action items By default Android displays the action items of your fragment behind those of the activity.

User PattaFeuFeu, setSubtitle(R.string.subtitle); // getSupportActionBar is not defined in the v4 version of says there should be no changes for the v4 fragment. onAttach() will cause a NullPointerException when the activity is rotated. Fragments and Tabs. There are several ways to setup tabs with fragments. The easiest is using ActionBar tabs. Note: Standard ActionBar tabs are not supported in Gingerbread, so many people use ActionBarSherlock when Gingerbread must be supported.

getSupportActionBar not available in Fragment; workaround leads to NullPointerException on rotation · android android-fragments appcompat  Android system simply destroy all the removed or replaced fragment if you didn't put the fragment transaction to the backstack. (2018) - 12 - Enable Fragment backward navigation using

Not able to sync an Exchange Account (Hotmail id) on Android phone since few days PremalMe , Nov 13, 2019 , in forum: Android Devices Replies:

Comments
  • As much as I hoped it would, that didn’t solve it. I updated my question and added the exception’s stack trace.
  • I had it in onCreate() before. Putting it in onActivityCreated() seems to work. I’ll try changing occurrences throughout the app; should it work everywhere, I’ll accept your answer. Thanks so far!
  • It worked. But shouldn’t getSupportActionBar() work "out of the box"?
  • It's working out of box, but since the getActivity return a generic Activity you have to upcast it for the desired class. Also be sure when using it you check if the getActivity() is an instance of the ActionBarActivity to avoid class cast exceptions
  • Actually I forgot to cast the activity to ActionBarActivity. Thanks Alex