GetActionBar () returns null after updating the SDK to 5.0

After updating the Android SDK to version 5.0, the getActionBar () method started to return null, which caused my application to crash on startup. I do not know what caused this, and any existing Stackoverflow threads did not 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 much appreciated!

+6
source share
2 answers

Use getSupportActionBar() when you use ActionBarActivity and appcompat-v7 , as you seem to be here.

+16
source

I just stumbled upon this myself. Not using AppCompat may not always be the solution, especially if you intend to use it. Therefore, if you are using AppCompatActivity, try pasting this into 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 ().

0
source

Source: https://habr.com/ru/post/977123/


All Articles