35

I have an activity with three fragment classes inside it. I get an error when trying to change the action bar title from inside of them. If I try to make the classes just public and not public static I get an error when I try to start that class. It should be pretty clear that the code is for preferences although that shouldn't change anything . Here's the code:

package com.simon.wikiics;

import android.preference.*;
import android.os.*;
import java.util.*;

public class MainSettingsActivity extends PreferenceActivity {

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

}

@Override
public void onBuildHeaders(List<Header> target) {
    loadHeadersFromResource(R.xml.headers, target);
}

//If I don't make the classes static my app force closes when I try to start them
public static class NavigationSettingsActivity extends PreferenceFragment {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.navigation);
        //The getActionBar() is what is giving me the error
        getActionBar().setTitle("Navigation");

    }
}

public static class InterfaceSettingsActivity extends PreferenceFragment {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.interf);
        //The getActionBar() is what is giving me the error
        getActionBar().setTitle("Interface");
    }

}

public static class OtherSettingsActivity extends PreferenceFragment {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.other);
        //The getActionBar() is what is giving me the error
        getActionBar().setTitle("Other");
    }
}
}
ahodder
  • 11,353
  • 14
  • 71
  • 114
SweSnow
  • 17,504
  • 10
  • 36
  • 49

5 Answers5

57

@Robert Estivil If you are using AppCompatActivity then use this:

actionBar = ((AppCompatActivity)getActivity()).getSupportActionBar();
Arul Pandian
  • 1,685
  • 15
  • 20
53

A static class cannot see private fields of another class. You will need to use getActivity().getActionBar() to retrieve it.

ahodder
  • 11,353
  • 14
  • 71
  • 114
  • Thanks! Just another question, how do I start an activity via an intent from inside this fragment class? – SweSnow Aug 17 '12 at 23:10
  • @SweSnow context.startActivity(new Intent(context, MyActivity.class)); – ahodder Aug 20 '12 at 05:36
  • I'm trying to turn off the action bar title using your approach (getActivity().getActionBar().setDisplayShowTitleEnabled(false)) but I'm getting a Null Pointer Exception. – Srikar Reddy Jul 27 '16 at 06:49
  • @SrikarReddy There are a ton of reasons this can happen. The most likely reason is that you are using an activity theme that does not include an action bar. I would double check that your theme is something like 'android:Theme.Holo.Light.DarkActionBar'. – ahodder Jul 27 '16 at 23:17
15

Though i am late in answering here. I found arul's answer perfect, but now ActionbarActivity is deprecated so slight modification to that answer will finish the job:

ActionBar actionBar = ((AppCompatActivity)getActivity()).getSupportActionBar();
Sourabh Kumar Sharma
  • 2,864
  • 3
  • 25
  • 33
3

With AppCompatActivity it is advised to shift to Toolbar instead of the action bar, But if you are not doing so then use this to get an instance of action bar.

((AppCompatActivity )getActivity()).getSupportActionBar();
Aniruddha K.M
  • 7,361
  • 3
  • 43
  • 52
0

if you use SherlokActionBar you can called your action bar using

ActionBar mActionBar = ((SherlockFragmentActivity) getActivity()).getSupportActionBar();
Mohamed Hussien
  • 1,084
  • 13
  • 14