getActionBar返回null

use*_*117 25 android android-actionbar

致电getActionBar回报null.这已屡有报道,所以我做了一定要包括其他人使用的解决方案:我的minSdkVersion=11,我有一个标题栏,和我打电话getActionBarsetContentView.此外,我的活动不是儿童活动.

setContentView(R.layout.main);

// experiment with the ActionBar 
ActionBar actionBar = getActionBar();
actionBar.hide();
Run Code Online (Sandbox Code Playgroud)

Device是运行Android 3.2的三星Galaxy Tab 10.1

提前感谢任何想法或建议!

zap*_*apl 59

看来你需要通过主题或下面的代码请求一个Actionbar(!=标题栏).

@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();
}
Run Code Online (Sandbox Code Playgroud)

来自[ 这里 ]的代码


Mar*_*ith 11

为了实现这一点,似乎需要满足几个条件.困扰我很久的那个是:

确保您的活动扩展了Activity(NOT ActionBarActivity).

public class MagicActivity extends Activity
Run Code Online (Sandbox Code Playgroud)

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

<application
    ...
    android:theme="@android:style/Theme.Holo" >
Run Code Online (Sandbox Code Playgroud)

并确保您的活动布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    ....
    android:theme="@android:style/Theme.WithActionBar" >
Run Code Online (Sandbox Code Playgroud)

-标记


小智 10

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


Him*_*ani 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" > 
Run Code Online (Sandbox Code Playgroud)