getActionBar()返回preferenceActivity(AppCompat-V7 21)空 [英] getActionBar() returns null in PreferenceActivity (AppCompat-v7 21)

查看:531
本文介绍了getActionBar()返回preferenceActivity(AppCompat-V7 21)空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经实现DoneBar(两个按钮中的动作条)的 preferenceActivity 为V20 SDK样品提供,但更新SDK和AppCompat到版本21我的应用程序崩溃后,在

I have implemented DoneBar (two buttons in actionbar) in PreferenceActivity as provided in v20 sdk samples, but after updating SDK and AppCompat to version 21 my app crashes at

显示java.lang.NullPointerException:尝试调用虚函数的空对象引用无效android.app.ActionBar.setDisplayOptions(INT,INT)

这是因为 getActionBar()返回null。并没有 getSupportActionBar() ActionBarActivity

That is because getActionBar() returns null. And There is no getSupportActionBar() as in ActionBarActivity.

所以我的问题是如何获得的 preferenceActivity 的动作条的对象,所以我可以申请它的自定义视图?

So my question is how to get that actionbar object in PreferenceActivity so I could apply custom view on it?

解决

经过一番研究,我设法通过使用来解决这个问题 preferenceFragment ActionBarActivity 所以我可以打电话 getSupportActionBar()

After some research I managed to solve this problem by using PreferenceFragment with ActionBarActivity so I could call getSupportActionBar()

推荐答案

为XXX要求我提供的例子,我怎么做的:

as xXx requested I am providing example how I done:

public class SettingsActivity extends ActionBarActivity {

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

        // Display the fragment as the main content.
        getFragmentManager().beginTransaction()
                .replace(android.R.id.content, new SettingsFragment())
                .commit();

        // use action bar here
        ActionBar actionBar = getSupportActionBar();
    }

    public static class SettingsFragment extends PreferenceFragment {
        @Override
        public void onCreate(Bundle paramBundle) {
            super.onCreate(paramBundle);
            addPreferencesFromResource(R.xml.pref_settings);
        }
    }

}

这篇关于getActionBar()返回preferenceActivity(AppCompat-V7 21)空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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