无法获取操作栏的片段类 [英] Can't get Action Bar in fragment class

查看:98
本文介绍了无法获取操作栏的片段类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个内部的三份片段类的活动。试图从他们的内部改变操作栏冠军时,我得到一个错误。如果我尽量让班级只是公众,而不是公共静态,我得到一个错误,当我尝试启动类。它应该是pretty的清楚,code是preferences尽管这不应该改变任何东西。这里的code:

 包com.simon.wikiics;

。进口的Andr​​oid preference *。
进口android.os *。
导入的java.util。*;

公共类MainSettingsActivity扩展preferenceActivity {

    @覆盖
    保护无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);

}

@覆盖
公共无效onBuildHeaders(名单<头>目标){
    loadHeadersFromResource(R.xml.headers,目标);
}

//如果我不作类的静态当我尝试启动它们我的应用程序的力量关闭
公共静态类NavigationSettingsActivity扩展preferenceFragment {

    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        加preferencesFromResource(R.xml.navigation);
        //该getActionBar()是什么让我的错误
        getActionBar()的setTitle(导航)。

    }
}

公共静态类InterfaceSettingsActivity扩展preferenceFragment {

    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        加preferencesFromResource(R.xml.interf);
        //该getActionBar()是什么让我的错误
        getActionBar()的setTitle(接口)。
    }

}

公共静态类OtherSettingsActivity扩展preferenceFragment {

    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        加preferencesFromResource(R.xml.other);
        //该getActionBar()是什么让我的错误
        getActionBar()的setTitle(其他)。
    }
}
}
 

解决方案

一个静态类不能看到另一个类的私有领域。您将需要使用 getActivity()。getActionBar()检索它。

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

解决方案

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

这篇关于无法获取操作栏的片段类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆