getActionBar() returns null after SDK update to 5.0

为君一笑 提交于 2019-12-21 12:30:04

问题


After I updated the Android SDK to version 5.0 the getActionBar() method started to return null causing my app to crash on launch. I am clueless to what's causing this and any existing Stackoverflow threads didn't help.

Here is my manifest:

<application
    android:name="App"
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/ActionBarTheme" >

styles.xml:

<style name="AppBaseTheme" parent="Theme.AppCompat.Light"></style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme"></style>
<style name="ListItemPadding">
    <item name="android:paddingTop">8dp</item>
    <item name="android:paddingRight">5dp</item>
    <item name="android:paddingBottom">8dp</item>
    <item name="android:paddingLeft">5dp</item>
</style>

themes.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">

<!-- the theme applied to the application or activity -->
<style name="ActionBarTheme" parent="@style/Theme.AppCompat.Light.DarkActionBar">
    <item name="android:actionBarStyle">@style/ActionBar</item>
    <item name="android:actionOverflowButtonStyle">@style/OverflowButton</item>
    <!-- Support library compatibility -->
    <item name="actionBarStyle">@style/ActionBar</item>
    <item name="actionOverflowButtonStyle">@style/OverflowButton</item>
</style>

<style name="OverflowButton" parent="android:style/Widget.Holo.ActionButton.Overflow">
    <item name="android:src">@drawable/menu</item>
</style>

<!-- ActionBar styles -->
<style name="ActionBar" parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
    <item name="android:background">@color/main</item>

    <!-- Support library compatibility -->
    <item name="background">@color/main</item>
</style>

</resources>

and my gradle dependencies:

dependencies {
compile 'com.github.jenzz.undobar:library:1.1:api8Release@aar'
compile 'com.android.support:support-v4:21.0.0'
compile 'com.android.support:appcompat-v7:21.0.0'
compile 'com.loopj.android:android-async-http:1.4.5'
compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.3'
compile 'com.google.android.gms:play-services:6.1.+'
}

Any help is greatly appreciated!


回答1:


Use getSupportActionBar() when you are using ActionBarActivity and appcompat-v7, as you appear to be doing here.




回答2:


I just came across this myself. Not using AppCompat might not always be a solution, especially if you intent to use it. Therefore, if using AppCompatActivity, try inserting this in your layout.xml:

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:layout_alignParentTop="true"
    android:background="?attr/colorPrimary"/>

Then, you can use getSupportActionBar().



来源:https://stackoverflow.com/questions/26526564/getactionbar-returns-null-after-sdk-update-to-5-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!