getActionBar返回nullgetActionBar返回null(getActionBar

2019-05-13 11:06发布

调用getActionBar返回null 。 这已屡有报道,所以我做了一定要包括其他人使用的解决方案:我minSdkVersion=11 ,我有一个标题栏,和我打电话getActionBarsetContentView 。 另外,我的行为是不是孩子的活动。

setContentView(R.layout.main);

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

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

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

Answer 1:

看来你需要或者通过一个主题,或通过下面的代码来请求具有动作条(!=标题栏)。

@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();
}

代码[ 这里 ]



Answer 2:

它似乎有其需要,以满足这个工作几个条件。 这难倒我了很久的一个是这样的:

请确保您的活动延伸活动(不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" >

-标记



Answer 3:

我好像通过切换getActionBar()getSupportActionBar()来解决我的所有错误。 我曾尝试不同的主题,加入getWindow()requestFeature(Window.FEATURE_ACTION_BAR)。 ,提出确保的setContentView(视图)是第一个和以前见过的所有其他的事情。



Answer 4:

操作栏需要的主题或活动与应用的标题在那里。 确保你没有风格的应用程序或活性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" > 


Answer 5:

我试图使用支持库的ActionBarActivity内的片段中得到的动作条。 我不得不这样做:

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

我的下一个任务是找出如何使这个通用的。 例如,这个代码感觉肮脏的,因为它需要我的片段都知道,它的使用由ActionBarActivity并使用支持库。 我认为它应该是一个检查的方法getActivity().getActionBar()第一,接着上述捕捉ClassCastException异常代码如果父活动不是ActionBarActivity。



Answer 6:

如果你为API14以上创建应用程序的项目,将有一个价值14文件夹中。 确保文件夹存在,它有一个styles.xml文件:

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

我得到这个错误,因为我创建了一个新的项目,我不希望在我的项目的任何额外价值的文件夹,所以我只是不停的值文件夹,删除的值-V11和值-V14夹。 但显然对于API14及以上的默认主题需要一个动作条在它的父标记,如果你想使用的动作条。 所以,我认为我能做到这一点不正确! 希望这可以帮助别人。 你最有可能与上述答案虽然解决您的问题。



Answer 7:

这个固定我的问题:

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


Answer 8:

真正的答案应该是既@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();
}


Answer 9:

请确保您延长ACTIONBARACTIVITY和不活动本应该解决您的问题



Answer 10:

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

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



Answer 11:

更改系统要求的顺序:

@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();
}


Answer 12:

In case when you are developing an app which is targeting sdk>=14 , and you are extending AppCompat to support Material theme in previous versions also . When you want to hide the items from your action bar like the back button you need to do this : In your activity (not in the fragment class) If you will you use getActionBar() it will return null. as in Appsupport theme getSupportAcionBar() will return the action bar.

Here is the link if you want to follow from DevByte

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

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




}


Answer 13:

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



文章来源: getActionBar returns null