【问题标题】:The getActionBar method returns null [duplicate]getActionBar 方法返回 null [重复]
【发布时间】:2014-10-23 21:44:07
【问题描述】:

我刚刚开始使用 Android 开发,并且正在学习 Google 的教程。我对操作栏有疑问,其中 getActionBar 方法返回 null,尽管根据 tutorial 它不应该返回。 我查看了类似的 StackOverflow 问题,但建议的解决方案均无效。

这是我的 onCreate 代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    getActionBar().setDisplayHomeAsUpEnabled(true);
}

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myfirstapp"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="21" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".DisplayMessageActivity"
            android:label="@string/title_activity_display_message"
            android:parentActivityName=".MainActivity" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.example.myfirstapp.MainActivity" />
        </activity>
    </application>

</manifest>

有什么想法吗?

编辑:添加 styles.xml(这是默认的,由 Eclipse 创建)

<resources>

    <!--
        Base application theme, dependent on API level. This theme is replaced
        by AppBaseTheme from res/values-vXX/styles.xml on newer devices.

    -->
    <style name="AppBaseTheme" parent="Theme.AppCompat.Light">
        <!--
            Theme customizations available in newer API levels can go in
            res/values-vXX/styles.xml, while customizations related to
            backward-compatibility can go here.
        -->
    </style>

    <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    </style>

</resources>

【问题讨论】:

  • 什么是@Style/AppTheme
  • Theme.AppCompat.Light

标签: android


【解决方案1】:

对于AppCompat v21

您的所有活动都必须从 ActionBarActivity 扩展

然后您使用getSupportActionBar() 检索操作栏。

【讨论】:

  • 我的活动已经扩展了 ActionBarActivity
  • 所以使用getSupportActionBar() 而不是getActionBar()
  • 成功了!你能解释一下为什么我需要使用 getSupportActionBar 而不是 getActionBar,尽管教程规定当 min SDK 为 11 或更高时应该使用 getActionBar?
  • 很遗憾,自 Android 5.0 (API 21) SDK 和 AppCompat v21 版本发布以来,部分文档尚未更新。由于 5.0 和之前版本的 Material Design 操作栏之间有相当大的变化,AppCompat 现在总是创建自己的操作栏,只能通过getSupportActionBar() 访问。这使您可以在较低的 API 级别上使用 5.0 中添加的 Action Bar 的许多很酷的新功能。
猜你喜欢
  • 2011-10-15
  • 1970-01-01
  • 1970-01-01
  • 2015-12-24
  • 1970-01-01
  • 1970-01-01
  • 2023-03-18
  • 1970-01-01
相关资源
最近更新 更多