2012-04-05 78 views
24

调用getActionBar返回null。这经常被报道,所以我确定要包括其他人使用的解决方案:我的minSdkVersion=11,我确实有一个标题栏,我在setContentView之后拨打getActionBar。另外,我的活动不是儿童活动。getActionBar返回null

setContentView(R.layout.main); 

// experiment with the ActionBar 
ActionBar actionBar = getActionBar(); 
actionBar.hide(); 

设备是三星Galaxy Tab 10.1运行Android 3.2

感谢您的任何意见或建议!

+0

参见:HTTP:/ /stackoverflow.com/questions/6867076/getactionbar-returns-null – Harvey 2014-11-05 21:02:47

回答

60

看来你需要通过主题或通过下面的代码请求有一个Actionbar(!= titlebar)。从[here]

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

    // The Action Bar is a window feature. The feature must be requested 
    // before setting a content view. Normally this is set automatically 
    // by your Activity's theme in your manifest. The provided system 
    // theme Theme.WithActionBar enables this for you. Use it as you would 
    // use Theme.NoTitleBar. You can add an Action Bar to your own themes 
    // by adding the element <item name="android:windowActionBar">true</item> 
    // to your style definition. 
    getWindow().requestFeature(Window.FEATURE_ACTION_BAR); 

    setContentView(R.layout.main); 

    // experiment with the ActionBar 
    ActionBar actionBar = getActionBar(); 
    actionBar.hide(); 
} 

代码

+0

谢谢。我以为我已经有一个操作栏,但我实际上是一个“系统栏” – user316117 2012-04-05 15:53:09

8

操作栏需要的主题或活动与应用的标题在那里。确保您没有将您的应用程序或活动设置为Theme.NOTITLE。

<application 
    android:name="com.xxx.yyy.Application" 
    android:debuggable="false" 
    android:icon="@drawable/icon" 
    android:label="@string/app_name" 
    android:theme="@style/Theme.NoTitle"> // remove this line if you have this in your code 


<activity 
     android:name="com.xxx.yyy.Activity" 
     android:configChanges="orientation|keyboardHidden|screenSize" 
     android:theme="@style/Theme.NoTitle" // remove this line if you have this in your code 
     android:windowSoftInputMode="adjustResize|stateHidden" > 
1

真正的答案应该是既@zapl和@Himanshu维尔马尼

对于Android清单组合:

<application 
    android:name="com.xxx.yyy.Application" 
    android:debuggable="false" 
    android:icon="@drawable/icon" 
    android:label="@string/app_name" 
    android:theme="@style/Theme.NoTitle"> // remove this line if you have this in your code 


<activity 
     android:name="com.xxx.yyy.Activity" 
     android:configChanges="orientation|keyboardHidden|screenSize" 
     android:theme="@style/Theme.NoTitle" // remove this line if you have this in your code 
     android:windowSoftInputMode="adjustResize|stateHidden" > 

的活动:

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

    // The Action Bar is a window feature. The feature must be requested 
    // before setting a content view. Normally this is set automatically 
    // by your Activity's theme in your manifest. The provided system 
    // theme Theme.WithActionBar enables this for you. Use it as you would 
    // use Theme.NoTitleBar. You can add an Action Bar to your own themes 
    // by adding the element <item name="android:windowActionBar">true</item> 
    // to your style definition. 
    getWindow().requestFeature(Window.FEATURE_ACTION_BAR); 
    setContentView(R.layout.activity_main); 
    // Hide the status bar for Android 4.1 and higher 
    View decorView = getWindow().getDecorView(); 
    // Hide the status bar. 
    int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN; 
    decorView.setSystemUiVisibility(uiOptions); 
    // Remember that you should never show the action bar if the 
    // status bar is hidden, so hide that too if necessary. 
    ActionBar actionBar = getActionBar(); 
    actionBar.hide(); 
} 
+0

对我来说,答案是将requestFeature调用移动到setContentView上方,谢谢! – Rickard 2014-06-19 19:32:40

2

如果创建应用程序项目为API14或更高版本,将会有一个值为14的文件夹。确保文件夹存在,它有一个styles.xml文件:

<style name="AppBaseTheme" parent="android:Theme.Holo.Light.DarkActionBar"> 
     <!-- API 14 theme customizations can go here. --> 
</style> 

,因为我创建了一个新的项目,我不希望在我的项目的任何额外价值的文件夹,我得到这个错误,所以我只是不停值文件夹并删除值-V11和值-V14文件夹。但显然,API14及以上版本的默认主题需要在其父XML标记中使用ActionBar,如果您想使用ActionBar。所以我认为我可以做到这一点不正确!希望这有助于某人。尽管如此,你最有可能通过上面的答案来解决你的问题。

4

我想从使用支持库的ActionBarActivity内的片段中获取ActionBar。我必须这样做:

ActionBarActivity actionBarActivity = (ActionBarActivity)getActivity(); 
ActionBar actionBar = actionBarActivity.getSupportActionBar(); 

我的下一个任务是弄清楚如何使这个通用。例如,这段代码感觉很脏,因为它需要我的Fragment知道它被ActionBarActivity使用并使用支持库。我认为它应该是一个方法,首先检查getActivity().getActionBar(),然后如果父Activity不是ActionBarActivity,则检查上面的代码捕获ClassCastException。

+0

只有这个答案适合我。即使我以为我是扩展ActionBarActivity,getActionBar()返回null :( – Vivek 2015-01-29 11:02:44

11

似乎有几个条件需要满足才能使其工作。长期困扰我的那个人是这样的:

确保你的活动扩展了Activity(不是ActionBarActivity)。

public class MagicActivity extends Activity 

确保您的应用清单具有类似

<application 
    ... 
    android:theme="@android:style/Theme.Holo" > 

,并确保您的活动布局有

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    .... 
    android:theme="@android:style/Theme.WithActionBar" > 

马克

1

确保您扩展ACTIONBARACTIVITY和不活动 这应该可以解决你的问题

9

我似乎通过切换getActionBar()getSupportActionBar()解决了我的所有错误。 我曾尝试过不同的主题,加入getWindow()。requestFeature(Window.FEATURE_ACTION_BAR);, 确保setContentView(view)是第一个以及之前见过的所有其他事物。

+0

作品完美!谢谢 – user2235615 2015-06-29 13:55:01

1

只是改变一点东西 1)清单文件编辑 机器人:主题= “@安卓风格/ Theme.Holo”>

2)片段文件中添加 机器人:主题=“@安卓风格/ Theme.WithActionBar”

1

更改系统请求的顺序:

@Override 
protected void onCreate(Bundle savedInstanceState) { 

    getWindow().requestFeature(Window.FEATURE_ACTION_BAR); 

    super.onCreate(savedInstanceState);  
    setContentView(R.layout.main); 

    // experiment with the ActionBar 
    ActionBar actionBar = getActionBar(); 
    actionBar.hide(); 
} 
0

在情况下,当你正在开发其靶向SDK> = 14的应用程序,并要扩展程序兼容性支持材料主题在以前versio ns也。当你想隐藏你的操作栏中的项目时,你需要这样做: 在你的活动中(不在片段类中) 如果你使用getActionBar(),它将返回null。如在Appsupport主题中,getSupportAcionBar()将返回操作栏。

这里是链接,如果你想从DevByte

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

    setContentView(R.layout.activity_detail); 
    getSupportActionBar().setDisplayHomeAsUpEnabled(false); 




} 
3

这个固定我的问题如下:

android.support.v7.app.ActionBar ab = getSupportActionBar(); 
0

也许你的活动的样式是基于NoActionBar