onCreate()抛出NULL指针异常

时间:2016-02-18 09:16:46

标签: android android-theme setcontentview appcompatactivity

我一直试图移植我的应用程序以支持前Lollipop设备,当我在pre L设备上运行应用程序时,操作栏似乎丢失了。这主要是因为getActionBar返回NULL并且我想从我在SO上读过的帖子,我应该转到getSupportActionBar(),我做了并改变了我的Splash Activity来使用这个主题:< / p>

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

但是当我的Splash Activity调用onCreate(Bundle)并调用setContentView()时,我现在得到以下异常:

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)

最初,我收到了You need to use a Theme.AppCompat theme (or descendant) with this activity中列出的错误,但在我更改了theme之后,如上所述,我遇到了上述崩溃问题。有人可以提供一些关于如何解决这个问题的指示吗?我找不到任何具体到我现在面临的问题。感谢。

更新:

下面是onCreate()中的Java代码设置:

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

2 个答案:

答案 0 :(得分:2)

替换

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

使用

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

}

答案 1 :(得分:0)

super应该是此方法的第一个调用。

您的方法应如下所示

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