Android getActionBar返回null

Android getActionBar返回null,android,android-actionbar,Android,Android Actionbar,调用getActionBar返回null。这经常被报道,所以我确保包括其他人使用的解决方案:我的minSdkVersion=11,我有一个标题栏,我在setContentView之后调用getActionBar。此外,我的活动不是儿童活动 setContentView(R.layout.main); // experiment with the ActionBar ActionBar actionBar = getActionBar(); actionBar.hide(); 该设备是运行安

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

setContentView(R.layout.main);

// experiment with the ActionBar 
ActionBar actionBar = getActionBar();
actionBar.hide();
该设备是运行安卓3.2的三星Galaxy Tab 10.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();
}
@覆盖
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//操作栏是一个窗口功能。必须请求该功能
//在设置内容视图之前。通常这是自动设置的
//根据清单中的活动主题。提供的系统
//theme.WithActionBar为您启用此功能。请随意使用
//使用Theme.NoTitleBar。您可以在自己的主题中添加操作栏
//通过添加元素true
//根据您的样式定义。
getWindow().requestFeature(Window.FEATURE\u ACTION\u栏);
setContentView(R.layout.main);
//使用ActionBar进行实验
ActionBar ActionBar=getActionBar();
actionBar.hide();
}

[]

操作栏中的代码需要有应用程序标题的主题或活动。确保您没有将应用程序或活动设置为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" > 
//如果代码中有此行,请删除此行

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

对于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();
}
@覆盖
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//操作栏是一个窗口功能。必须请求该功能
//在设置内容视图之前。通常这是自动设置的
//根据清单中的活动主题。提供的系统
//theme.WithActionBar为您启用此功能。请随意使用
//使用Theme.NoTitleBar。您可以在自己的主题中添加操作栏
//通过添加元素true
//根据您的样式定义。
getWindow().requestFeature(Window.FEATURE\u ACTION\u栏);
setContentView(R.layout.activity_main);
//隐藏Android 4.1及更高版本的状态栏
View decorView=getWindow().getDecorView();
//隐藏状态栏。
int uiOptions=View.SYSTEM\u UI\u FLAG\u全屏显示;
decorView.设置系统兼容性(uiOptions);
//请记住,如果
//状态栏是隐藏的,所以在必要时也隐藏它。
ActionBar ActionBar=getActionBar();
actionBar.hide();
}

如果您为API14或更高版本创建应用程序项目,将有一个values-14文件夹。确保文件夹存在,并且它有一个styles.xml文件,其中包含:

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


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

我试图使用支持库从ActionBarActivity中的一个片段中获取ActionBar。我必须这样做:

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

我的下一个任务是找出如何使这个通用。例如,这段代码感觉脏,因为它要求我的片段知道它正在被ActionBarActivity使用,并且正在使用支持库。我认为,如果父活动不是ActionBarActivity,则应该首先检查
getActivity().getActionBar()
,然后检查上面的代码,以捕获ClassCastException。

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

public class MagicActivity extends Activity
确保您的活动扩展了活动(而不是ActionBarActivity)

请确保您的应用程序清单包含以下内容

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

并确保您的活动布局

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


-标记

确保扩展ACTIONBARACTIVITY而不是ACTIVITY
这将解决您的问题

我似乎通过使用getSupportActionBar()切换getActionBar()解决了所有错误。 我尝试了不同的主题,添加了getWindow().requestFeature(Window.FEATURE\u ACTION\u BAR),, 确保setContentView(视图)是第一个,所有其他内容都是之前看到的

只需改变两件事 1) 清单文件编辑 android:theme=“@android:style/theme.Holo”>

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

更改系统请求的顺序:

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

如果您正在开发一个以sdk>=14为目标的应用程序,并且您正在扩展AppCompat以支持以前版本中的材质主题。如果要从操作栏(如“后退”按钮)隐藏项目,则需要执行以下操作: 在您的活动中(不在fragment类中) 如果使用getActionBar(),它将返回null。与Appsupport主题一样,getSupportCionBar()将返回操作栏

这里是链接,如果你想跟随从

这解决了我的问题:

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

可能您的活动的样式基于NoActionBar

步骤01:

改变你的班级活动到应用程序活动

i、 e:

步骤02:

创建一个全局变量,如下所示。(我使用的是androidx。看起来就是这样)

步骤03:

使用暗色操作栏,如下所示

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Your style -->
</style>
步骤05:

现在你可以用actionBar做任何你想做的事情

i、 e

谢谢我
public class BluetoothActivity extends AppCompatActivity {
//Your Code
}
androidx.appcompat.app.ActionBar actionBar;
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Your style -->
</style>
setContentView(R.layout.activity_bluetooth);
actionBar = getSupportActionBar(); //Create this After setContentView
actionBar.show(); //this is for Experimental process only
actionBar.setTitle("Profile");