getActionBar()返回null [英] getActionBar() returning null

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

问题描述

我需要我的动作条是的setContentView前准备好,因为它使用的navDrawerFragment但在这一点:

 公共类BaseActivity扩展活动实现NavigationDrawerFragment.NavigationDrawerCallbacks {@覆盖
保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_ACTION_BAR);
    Log.d(,getActionBar()的toString());
    的setContentView(R.layout.activity_base);
}

据返回null

我的主题是:

 <样式名称=AppTheme父=Theme.AppCompat.Light.DarkActionBar>
    <项目名称=colorPrimary> @彩色/ light_blue< /项目>
    <项目名称=colorPrimaryDark> @彩色/ dark_blue< /项目>
    <项目名称=colorAccent> @彩色/ dark_blue< /项目>
    <项目名称=机器人:windowBackground> @色/白< /项目>
    <项目名称=机器人:positiveButtonText> @色/白< /项目>
    <项目名称=机器人:windowActionBar>真< /项目>
< /风格>

和报关清单时:

 <活动
    机器人:名字=。controller.activity.BaseActivity
    机器人:标签=@字符串/ APP_NAME>
< /活性GT;


解决方案

  1. BaseActivity 必须扩展 ActionBarActivity 而不是活动

    公共类BaseActivity扩展ActionBarActivity实现NavigationDrawerCallbacks {


  2. 使用 getSupportActionBar(); 来获得动作条


I need my actionbar to be ready before setContentView because it is used by the navDrawerFragment but at this point:

public class BaseActivity extends Activity implements NavigationDrawerFragment.NavigationDrawerCallbacks {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_ACTION_BAR);
    Log.d("", getActionBar().toString());
    setContentView(R.layout.activity_base);
}

It is returning null

My theme:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="colorPrimary">@color/light_blue</item>
    <item name="colorPrimaryDark">@color/dark_blue</item>
    <item name="colorAccent">@color/dark_blue</item>
    <item name="android:windowBackground">@color/white</item>
    <item name="android:positiveButtonText">@color/white</item>
    <item name="android:windowActionBar">true</item>
</style>

and the declaration at manifest:

<activity
    android:name=".controller.activity.BaseActivity"
    android:label="@string/app_name" >
</activity>

解决方案

  1. Your BaseActivity must extends from ActionBarActivity and not Activity

    public class BaseActivity extends ActionBarActivity implements NavigationDrawerCallbacks {

  2. Use getSupportActionBar(); to get the ActionBar

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

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