Can't get Action Bar in fragment class

2019-02-05 13:41发布

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");
    }
}
}

5条回答
forever°为你锁心
2楼-- · 2019-02-05 14:04

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

actionBar = ((ActionBarActivity)getActivity()).getSupportActionBar();
查看更多
虎瘦雄心在
3楼-- · 2019-02-05 14:13

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

查看更多
孤傲高冷的网名
4楼-- · 2019-02-05 14:16

if you use SherlokActionBar you can called your action bar using

ActionBar mActionBar = ((SherlockFragmentActivity) getActivity()).getSupportActionBar();
查看更多
Fickle 薄情
5楼-- · 2019-02-05 14:21

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();
查看更多
我欲成王,谁敢阻挡
6楼-- · 2019-02-05 14:26

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();
查看更多
登录 后发表回答