Search code examples
androidandroid-themesetcontentviewappcompatactivity

onCreate() throws NULL Pointer exception


I have been trying to port my application to support pre Lollipop devices and the action bar seems to be missing when I run the application on pre L device. It was mainly because getActionBar was returning NULL and I think from the posts I have read on SO, I was supposed to move onto getSupportActionBar(), which I did and changed my Splash Activity to use this theme:

    <style name="AppTheme" parent="Theme.AppCompat">
    <item name="windowNoTitle">true</item>
    <item name="windowActionBar">false</item>
    </style>

But when onCreate(Bundle) gets invoked for my Splash Activity and setContentView() is called, I now get the following exception:

02-18 14:38:42.750 27331-27331/com.airwatch.tunnel E/AndroidRuntime: FATAL EXCEPTION: main
                                                                 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.airwatch.tunnel/com.airwatch.tunnel.ui.activities.SplashActivity}: java.lang.NullPointerException
                                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
                                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
                                                                     at android.app.ActivityThread.access$600(ActivityThread.java:141)
                                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
                                                                     at android.os.Handler.dispatchMessage(Handler.java:99)
                                                                     at android.os.Looper.loop(Looper.java:137)
                                                                     at android.app.ActivityThread.main(ActivityThread.java:5103)
                                                                     at java.lang.reflect.Method.invokeNative(Native Method)
                                                                     at java.lang.reflect.Method.invoke(Method.java:525)
                                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
                                                                     at dalvik.system.NativeStart.main(Native Method)
                                                                  Caused by: java.lang.NullPointerException
                                                                     at android.support.v7.app.AppCompatDelegateImplV7.applyFixedSizeWindow(AppCompatDelegateImplV7.java:487)
                                                                     at android.support.v7.app.AppCompatDelegateImplV7.ensureSubDecor(AppCompatDelegateImplV7.java:287)
                                                                     at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:253)
                                                                     at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109)

Initially, I was getting errors as listed in You need to use a Theme.AppCompat theme (or descendant) with this activity , but after I changed the theme as described above, I am getting the above crashes. Can someone please provide some pointers on how to go about fixing this? I couldn't find anything specific to the problem I am facing right now. Thanks.

UPDATE:

Below is the Java code setup in onCreate():

@Override
protected void onCreate(Bundle arg0) {
    setContentView(R.layout.activity_splash);
    initViews();
    mProfileReadyReceiver.registerReceiver();
    super.onCreate(arg0);
}

Solution

  • Replace

    @Override
    protected void onCreate(Bundle arg0) {
        setContentView(R.layout.activity_splash);
        initViews();
        mProfileReadyReceiver.registerReceiver();
        super.onCreate(arg0);
    }
    

    With

    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        setContentView(R.layout.activity_splash);
        initViews();
        mProfileReadyReceiver.registerReceiver();
    
    }