0

I am trying to add a splash screen to my app to display while everything is loading. I followed this post to do this via a theme. It looks to be working the way I want, but shortly after the splash is displayed the app crashes when trying to customize the ActionBar.

theme.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="Theme.SplashScreen" parent="@style/Theme.AppCompat">        
        <item name="android:windowBackground">@drawable/orange_background</item>
        <item name="android:windowNoTitle">true</item>
    </style>
</resources>

Relevant section of AndroidManifest.xml

<application
    android:allowBackup="true"
    android:theme="@style/CustomActionBarTheme" 
    android:label="@string/app_name"
    android:icon="@drawable/ic_launcher"
    android:largeHeap="true" >
    <activity
        android:name="com.example.app.MainActivity"
        android:label="@string/app_name"
        android:theme="@style/Theme.SplashScreen"
        android:screenOrientation="portrait" >

Section in MainActivity.java causing crash

final ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);

Caused by: java.lang.NullPointerException at android.support.v7.app.ActionBarImplICS.setDisplayHomeAsUpEnabled(ActionBarImplICS.java:174) at android.support.v7.app.ActionBarImplJB.setDisplayHomeAsUpEnabled(ActionBarImplJB.java:20)

NOTE: This crash only started happening after I implemented the splash screen.

Kevin_TA
  • 4,575
  • 13
  • 48
  • 77

3 Answers3

3

I guess it's a conflict with windowNoTitle and setDisplayHomeAsUpEnable. For the splash screen, you disable the Title(and the full ActionBar), and in MainActivity.java you try to set an up button in the disabled ActionBar.

You might want to use the code from this post.

Community
  • 1
  • 1
fifarunnerr
  • 617
  • 4
  • 12
  • This looks very similar to [this question](http://stackoverflow.com/questions/18602953/getactionbar-setdisplayhomeasupenabledtrue-throws-nullpointerexception-on-n), so this might indeed be the cause. – andersschuller Jan 20 '14 at 20:02
3

why do you need to get action bar in splash screen?

once you define:

 <item name="android:windowNoTitle">true</item>

getActionBar always returns null

vipul mittal
  • 17,343
  • 3
  • 41
  • 44
2

Adding "android:windowNoTitle" means no action bar is created. Therefore getSupportActionBar() will return null.

ianhanniballake
  • 191,609
  • 30
  • 470
  • 443
  • Ah ok. I just kept that from the template and not being an Android developer it wasn't immediately alarming to me. Getting rid of that line however shows the title bar on the splash screen which i do not want. – Kevin_TA Jan 20 '14 at 20:13
  • @Kevin_TA - if you don't want to show the action bar, why are you calling `getSupportActionBar()` in the first place? Seems like if you don't want it to display then it doesn't matter if the home button is enabled on the bar that isn't visible... – ianhanniballake Jan 20 '14 at 20:18
  • I need the action bar in my main activity which is shown after the splash screen. I just need to hide the action bar for the splash screen, then display it for the rest of the app. – Kevin_TA Jan 20 '14 at 20:19
  • That's one of the reasons why Splash Screens (which are generally considered a [bad idea](https://plus.google.com/u/0/+EricRichardson/posts/cgoZN7QbCSF)) are implemented as separate activities - so you can separate themes for them vs the rest of your application. – ianhanniballake Jan 20 '14 at 20:38