该getActionBar方法返回null [英] The getActionBar method returns null
问题描述
我刚刚接触Android的发展,我下面的教程由谷歌。我已经用行动吧,那里的getActionBar方法返回null一个问题,虽然具体根据的教程的它不应该。 我检查了类似计算器的问题,但没有提出的解决方案的工作。
下面是我的onCreate code:
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
getActionBar()setDisplayHomeAsUpEnabled(真)。
}
AndroidManifest.xml中:
< XML版本=1.0编码=UTF-8&GT?;
<舱单的xmlns:机器人=http://schemas.android.com/apk/res/android
包=com.example.myfirstapp
安卓版code =1
机器人:VERSIONNAME =1.0>
<用途-SDK
安卓的minSdkVersion =11
机器人:targetSdkVersion =21/>
<应用
机器人:allowBackup =真
机器人:图标=@可绘制/ ic_launcher
机器人:标签=@字符串/ APP_NAME
机器人:主题=@风格/ AppTheme>
<活动
机器人:名称=。MainActivity
机器人:标签=@字符串/ APP_NAME>
<意向滤光器>
<作用机器人:名称=android.intent.action.MAIN/>
<类机器人:名称=android.intent.category.LAUNCHER/>
&所述; /意图滤光器>
< /活性GT;
<活动
机器人:名称=。DisplayMessageActivity
机器人:标签=@字符串/ title_activity_display_message
机器人:parentActivityName =MainActivity。>
&所述;元数据
机器人:名称=android.support.PARENT_ACTIVITY
机器人:值=com.example.myfirstapp.MainActivity/>
< /活性GT;
< /用途>
< /舱单>
任何想法?
编辑:添加styles.xml(这是默认的,由Eclipse的创建)
<资源>
<! -
基本应用的主题,依赖于API级别。这个主题被替换
通过AppBaseTheme从RES /值-VXX /上较新的设备styles.xml。
- >
<样式名称=AppBaseTheme父=Theme.AppCompat.Light>
<! -
在新的API级别可以在可用主题自定义
RES /值-VXX / styles.xml,而自定义相关
向后兼容性可以去这里。
- >
< /风格>
<! - 应用主题。 - >
<样式名称=AppTheme父=AppBaseTheme>
<! - 所有的定制,这不是针对特定的API级别的可以去这里。 - >
< /风格>
< /资源>
有关<一href="http://android-developers.blogspot.com/2014/10/appcompat-v21-material-design-for-$p$p.html">AppCompat V21 :
你的所有活动必须扩展从ActionBarActivity
您再使用<一个href="https://developer.android.com/reference/android/support/v7/app/ActionBarActivity.html#getSupportActionBar()">getSupportActionBar()检索操作栏。
I'm just getting started with Android development and I'm following the tutorials by Google. I have a problem with action bars, where the getActionBar method returns null, although as per the tutorial it shouldn't. I reviewed similar StackOverflow questions, but none of the proposed solutions works.
Here is my onCreate code:
@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>
Any ideas?
EDIT: adding styles.xml (it's the default one, created by 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>
For AppCompat v21:
All of your Activities must extend from ActionBarActivity
You then use getSupportActionBar() to retrieve the Action Bar.
这篇关于该getActionBar方法返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!