Android getActionBar 返回 null

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10031180/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 02:15:39  来源:igfitidea点击:

getActionBar returns null

androidandroid-actionbar

提问by user316117

Calling getActionBarreturns null. This has been frequently reported so I've made sure to include the solutions others have used: My minSdkVersion=11, I do have a titlebar, and I'm calling getActionBarafter setContentView. Also, my activity is not a child activity.

调用getActionBar返回null。这经常被报告,所以我确保包括其他人使用的解决方案:我的minSdkVersion=11,我有一个标题栏,我getActionBarsetContentView. 此外,我的活动不是儿童活动。

setContentView(R.layout.main);

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

Device is a Samsung Galaxy Tab 10.1 running Android 3.2

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

Thanks in advance for any ideas or suggestions!

在此先感谢您的任何想法或建议!

回答by zapl

It seems you need to request having an Actionbar (!= titlebar) either via a Theme or via below code.

似乎您需要通过主题或通过以下代码请求拥有操作栏(!= 标题栏)。

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

Code from [here]

来自[这里]的代码

回答by Mark Smith

It seems there are several conditions which need to be met in order for this to work. The one which stumped me for a long time was this:

似乎有几个条件需要满足才能使其发挥作用。困扰我很久的一个是:

Make sure your activity extends Activity (NOT ActionBarActivity).

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

public class MagicActivity extends Activity

Make sure your app manifest has something like

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

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

and make sure your activity layout has

并确保您的活动布局具有

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

-Mark

-标记

回答by ContinuousError

I seemed to solve all my errors by switching getActionBar()with getSupportActionBar(). I had tried different themes, adding getWindow().requestFeature(Window.FEATURE_ACTION_BAR);, made sure the setContentView(view) was first and all the other things seen before.

我似乎通过将getActionBar()getSupportActionBar()切换来解决所有错误。我尝试了不同的主题,添加getWindow().requestFeature(Window.FEATURE_ACTION_BAR); ,确保 setContentView(view) 是第一个,并且之前看到的所有其他东西。

回答by Himanshu Virmani

Action bar needs theme or activity with app title to be there. Make sure you have not styled your application or activity as Theme.NOTITLE.

操作栏需要带有应用标题的主题或活动。确保您没有将您的应用程序或活动设置为 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" > 

回答by Harvey

I was trying to get the ActionBar from within a fragment inside an ActionBarActivity using the support library. I had to do this:

我试图使用支持库从 ActionBarActivity 内的片段中获取 ActionBar。我不得不这样做:

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

My next quest is to figure out how to make this generic. For example, this code feels dirty because it requires my Fragment to know both that it's being used by an ActionBarActivity and using the support library. I think it should be a method that checks getActivity().getActionBar()first followed by the code above catching the ClassCastException if the parent Activity is not an ActionBarActivity.

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

回答by Jayakrishnan PM

This fixed my issue:

这解决了我的问题:

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

回答by Lou Morda

If you create an application project for API14 or above, there will be a values-14 folder. Make sure that folder exists and it has a styles.xml file with:

如果创建 API14 或以上的应用程序项目,将会有一个 values-14 文件夹。确保该文件夹存在并且它有一个 style.xml 文件,其中包含:

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

I got this error because I created a new project and I didn't want any extra values folders in my project, so I just kept the values folder and removed the values-v11 and values-v14 folders. But apparently the default theme for API14 and above requires an ActionBar in it's parent XML tag if you want to use the ActionBar. So I assumed I could do that incorrectly! Hope this helps somebody. Your most likely to resolve your issues with the above answers though.

我收到此错误是因为我创建了一个新项目并且我不想在我的项目中使用任何额外的值文件夹,所以我只保留了值文件夹并删除了值-v11 和值-v14 文件夹。但显然,如果您想使用 ActionBar,API14 及更高版本的默认主题需要在其父 XML 标记中包含一个 ActionBar。所以我认为我可能做错了!希望这可以帮助某人。不过,您最有可能通过上述答案解决您的问题。

回答by nnhthuan

The real answer should be combination of both @zapl and @Himanshu Virmani

真正的答案应该是@zapl 和@Himanshu Virmani 的结合

For Android Manifest:

对于 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" > 

For Activity:

对于活动:

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

回答by Kamal Bunkar

just change two thing 1) manifest file edit android:theme="@android:style/Theme.Holo" >

只需更改两件事 1) 清单文件编辑 android:theme="@android:style/Theme.Holo">

2) Fragment file add android:theme="@android:style/Theme.WithActionBar"

2) 片段文件添加 android:theme="@android:style/Theme.WithActionBar"

回答by Srikrishnan

Change the order of system request:

更改系统请求的顺序:

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