0

I am getting a NullPointerException as below:

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

I have tried the solutions mentioned in other posts but that doesn't seem to help. I do feel that the issue is around the appcompat version. But strange thing to note is that i have seen the issue appearing only after introducing Firebase library version 9.8.0. Not sure how this is connected.

Code snipper where error is thrown:

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

App Gradle file content is shown below:

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 content:

<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>

Note that the issue not related to instantiation. The issue is observed only when I have below three lines for Firebase library in the gradle file else all works fine.

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'

I don't see this issue when I use older version of firebase libraries below.

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'

This issue seems strange and not sure how to fix this. I need higher version of firebase as sendEmailVerification is supported only in this higher version.

dacscan3669
  • 651
  • 2
  • 8
  • 16

1 Answers1

0

setUserVisibleHint() , is executed after onCreate() but you have to instantiate views in onCreate() method of Activity ,other wise it will throw NPE.

Ratilal Chopda
  • 4,162
  • 4
  • 18
  • 31
himel
  • 500
  • 5
  • 14