Android NullPointerException尝试调用虚方法'android.support.v7.app.ActionBar MainActivity.getSupportActionBar()

时间:2017-11-07 04:08:11

标签: java android nullpointerexception android-actionbar

我得到NullPointerException,如下所示:

NullPointerException: Attempt to invoke virtual method 'android.support.v7.app.ActionBar MainActivity.getSupportActionBar()

我已尝试过其他帖子中提到的解决方案,但似乎没有帮助。我觉得问题出在appcompat版本附近。但值得注意的是,我看到问题只是在介绍Firebase库版本9.8.0之后才出现。不确定这是如何连接的。

抛出错误的代码片段:

@Override
   public void setUserVisibleHint(boolean isVisibleToUser) {
      super.setUserVisibleHint(isVisibleToUser);
       if (isVisibleToUser) {
        ((MainActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(false);
        ((MainActivity) getActivity()).setToolBarTitle(FragTitle);
    }
  }

App Gradle文件内容如下所示:

apply plugin: 'com.android.application'

android {
compileSdkVersion 23
buildToolsVersion '25.0.0'

defaultConfig {
  applicationId "com.pack.App1"
  minSdkVersion 16
  targetSdkVersion 23
  versionCode 1
  versionName "1.0"
}

buildTypes {
release {
  minifyEnabled false
  proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
 }
}
lintOptions {
abortOnError false
 }
}

repositories {
  maven { url "https://jitpack.io" }
}

dependencies {
 compile fileTree(dir: 'libs', include: ['*.jar'])
 testCompile 'junit:junit:4.12'
 compile 'com.android.support:appcompat-v7:23.1.1'
 compile 'com.squareup.picasso:picasso:2.5.0'
 compile 'com.google.firebase:firebase-database:9.8.0'
 compile 'com.google.firebase:firebase-storage:9.8.0'
 compile 'com.google.firebase:firebase-auth:9.8.0'
 compile 'com.android.support:cardview-v7:23.0.+'
 compile 'com.android.support:recyclerview-v7:23.0.+'
 compile ('com.android.support:appcompat-v7:23.1.0') { force=true; }
 compile 'com.android.support:design:23.0.1'
 compile 'com.github.PhilJay:MPAndroidChart:v2.2.4'
 compile 'com.google.android.gms:play-services-auth:9.8.0'
}

apply plugin: 'com.google.gms.google-services'

Styles.xml内容:

<resources>

  <!-- Base application theme. -->
  <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
  </style>

  <style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
    <item name="android:windowBackground">@drawable/background_splash</item>
  </style>

  <style name="Theme.AppCompat.Light.NoActionBar.FullScreen" parent="@style/Theme.AppCompat.Light">
    <item name="windowNoTitle">true</item>
    <item name="windowActionBar">false</item>
    <item name="android:windowFullscreen">true</item>
    <item name="android:windowContentOverlay">@null</item>

  </style>

  <style name="MyMaterialTheme" parent="MyMaterialTheme.Base">

  </style>

  <style name="MyMaterialTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="windowNoTitle">true</item>
    <item name="windowActionBar">false</item>
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
  </style>

</resources>

请注意,该问题与实例化无关。只有当我在gradle文件中有三行Firebase库时才会出现此问题,否则一切正常。

compile 'com.google.firebase:firebase-database:9.8.0'
compile 'com.google.firebase:firebase-storage:9.8.0'
compile 'com.google.firebase:firebase-auth:9.8.0'

当我在下面使用旧版本的firebase库时,我没有看到这个问题。

compile 'com.google.firebase:firebase-database:9.2.1'
compile 'com.google.firebase:firebase-storage:9.2.1'
compile 'com.google.firebase:firebase-auth:9.2.1'

这个问题看起来很奇怪,不知道如何解决这个问题。我需要更高版本的firebase,因为只有在更高版本中才支持sendEmailVerification。

1 个答案:

答案 0 :(得分:0)

setUserVisibleHint(),在onCreate()之后执行,但您必须在onCreate()的{​​{1}}方法中实例化视图,否则它将抛出Activity