Android 无法在片段类中获取操作栏
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12013849/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can't get Action Bar in fragment class
提问by SweSnow
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");
}
}
}
回答by Arul Pandian
@Robert Estivil
If you are using AppCompatActivity
then use this:
@Robert Estivil 如果您正在使用,请AppCompatActivity
使用以下命令:
actionBar = ((AppCompatActivity)getActivity()).getSupportActionBar();
回答by ahodder
A static class cannot see private fields of another class. You will need to use getActivity().getActionBar()
to retrieve it.
静态类无法看到另一个类的私有字段。您将需要使用getActivity().getActionBar()
来检索它。
回答by Sourabh Kumar Sharma
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:
虽然我在这里回答晚了。我发现 arul 的答案很完美,但现在 ActionbarActivity 已被弃用,因此对该答案稍加修改即可完成工作:
ActionBar actionBar = ((AppCompatActivity)getActivity()).getSupportActionBar();
回答by Mightian
With AppCompatActivity it is advised to shift to Toolbarinstead of the action bar, But if you are not doing so then use this to get an instance of action bar.
使用 AppCompatActivity 建议转移到工具栏而不是操作栏,但如果您不这样做,则使用它来获取操作栏的实例。
((AppCompatActivity )getActivity()).getSupportActionBar();
回答by Mohamed Hussien
if you use SherlokActionBar you can called your action bar using
如果您使用 SherlokActionBar,您可以使用
ActionBar mActionBar = ((SherlockFragmentActivity) getActivity()).getSupportActionBar();