简体   繁体   English

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

[英]Android NullPointerException Attempt to invoke virtual method 'android.support.v7.app.ActionBar MainActivity.getSupportActionBar()

I am getting a NullPointerException as below: 我收到如下所示的NullPointerException

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. 我确实认为问题出在appcompat版本附近。 But strange thing to note is that i have seen the issue appearing only after introducing Firebase library version 9.8.0. 但是要注意的奇怪的是,我只在引入Firebase库版本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: 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 content: 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>

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. 仅当我在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'

I don't see this issue when I use older version of firebase libraries below. 当我在下面使用旧版本的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'

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. 我需要更高版本的firebase,因为sendEmailVerification仅在更高版本中受支持。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android 错误 [尝试调用虚拟方法 &#39;void android.support.v7.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)] - Android Error [Attempt to invoke virtual method 'void android.support.v7.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)] 尝试调用虚拟方法&#39;android.content.Context android.support.v4.app.FragmentActivity.getApplicationContext()&#39;android - Attempt to invoke virtual method 'android.content.Context android.support.v4.app.FragmentActivity.getApplicationContext()' android Android错误:类型不匹配无法从android.app.actionbar转换为android.support.v7.app.actionbar - Android Error: type mismatch cannot convert from android.app.actionbar to android.support.v7.app.actionbar android尝试在null对象Referenc上调用虚拟方法&#39;void android.app.ActionBar.setHomeButtonEnabled(boolean)&#39; - android Attempt to invoke virtual method 'void android.app.ActionBar.setHomeButtonEnabled(boolean)' on a null object referenc java.lang.NullPointerException:尝试在空对象引用上调用虚方法'android.view.View .MainActivity.findViewById(int)' - java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View .MainActivity.findViewById(int)' on a null object reference 尝试在空对象引用上调用虚拟方法“android.support.v7.app.AlertDialog android.support.v7.app.AlertDialog$Builder.create()” - Attempt to invoke virtual method 'android.support.v7.app.AlertDialog android.support.v7.app.AlertDialog$Builder.create()' on a null object reference 尝试在空对象引用上调用虚方法&#39;void android.app.ActionBar.setHomeButtonEnabled(boolean)&#39; - Attempt to invoke virtual method 'void android.app.ActionBar.setHomeButtonEnabled(boolean)' on a null object reference Android Studio java.lang.NullPointerException:尝试调用虚拟方法 - Android Studio java.lang.NullPointerException: Attempt to invoke virtual method Android Studio:NullPointerException:尝试在 null object 参考上调用虚拟方法“...” - Android Studio: NullPointerException: Attempt to invoke virtual method '...' on a null object reference 尝试调用虚方法 &#39;void android.support.v7.widget.RecyclerView.setLayoutManager(android.support.v7.widget.RecyclerView$LayoutManager - Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setLayoutManager(android.support.v7.widget.RecyclerView$LayoutManager
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM