Firebase Auth获取空指针异常。已更新依赖项。降级,但仍无法正常运行

时间:2018-07-16 05:51:43

标签: android firebase firebase-authentication

我不断 java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'java.lang.String com.google.firebase.auth.FirebaseUser.getUid()'。

我已经测试了降级依赖项,清理项目。 我的互联网连接很好。但是该应用在每次启动时都会崩溃。

一些成功的登录后,我开始的新项目也遇到了同样的问题。 :(

我的代码:

private FirebaseAuth mAuth;
private FirebaseUser mUser;
private FirebaseFirestore mFirestore;
private FirebaseAuth.AuthStateListener mAuthListener;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ButterKnife.bind(this);
    setSupportActionBar(mToolbar);

    //firebase
    mAuth = FirebaseAuth.getInstance();
    mAuth.addAuthStateListener(mAuthListener);
    mFirestore = FirebaseFirestore.getInstance();
    mUser = FirebaseAuth.getInstance().getCurrentUser();
    authListner();


    initList();

    String userId = mAuth.getCurrentUser().getUid();

    //login credentials
    // Configure Google Sign In
    GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestIdToken(getString(R.string.default_web_client_id))
            .requestEmail()
            .build();
    // Build a GoogleSignInClient with the options specified by gso.
    mGoogleSignInClient = GoogleSignIn.getClient(this, gso);

}

我的礼物:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 'android-P'
    buildToolsVersion "27.0.3"

    defaultConfig {
        applicationId "com.job.carwash_getfreewashescoupons"
        minSdkVersion 19
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

        //Enabling multidex support.
        multiDexEnabled true
        vectorDrawables.useSupportLibrary = true

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

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    //noinspection GradleCompatible
    implementation 'com.android.support:appcompat-v7:28.0.0-alpha1'
    implementation 'com.android.support:support-vector-drawable:28.0.0-alpha1'
    implementation 'com.android.support:support-v4:28.0.0-alpha1'
    implementation 'com.android.support:design:28.0.0-alpha1'
    implementation 'com.android.support:cardview-v7:28.0.0-alpha1'
    implementation 'com.android.support.constraint:constraint-layout:1.1.2'

    //If your minSdkVersion is lower than 21
    implementation 'com.android.support:multidex:1.0.3'

    //
    implementation 'com.google.firebase:firebase-core:16.0.1'
    implementation 'com.google.firebase:firebase-firestore:17.0.3'
    implementation 'com.google.firebase:firebase-auth:16.0.2'
    implementation 'com.google.android.gms:play-services-auth:15.0.1'
    implementation 'com.google.firebase:firebase-messaging:17.1.0'

    // FirebaseUI for Cloud Firestore
    implementation 'com.firebaseui:firebase-ui-firestore:4.1.0'

    // ButterKnife (for code brevity)
    implementation 'com.jakewharton:butterknife:8.8.1'
    annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'

    //...

}

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

Logcat:

07-16 08:39:33.014 2030-2030/? E/art: Failed to send JDWP packet APNM to debugger (-1 of 99): Broken pipe
07-16 08:39:33.187 2030-2047/? E/art: Failed sending reply to debugger: Broken pipe
07-16 08:39:35.396 2030-2030/com.job.carwash_getfreewashescoupons E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.job.carwash_getfreewashescoupons, PID: 2030
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.job.carwash_getfreewashescoupons/com.job.carwash_getfreewashescoupons.ui.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.google.firebase.auth.FirebaseUser.getUid()' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2521)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2601)
        at android.app.ActivityThread.access$800(ActivityThread.java:178)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1470)
        at android.os.Handler.dispatchMessage(Handler.java:111)
        at android.os.Looper.loop(Looper.java:194)
        at android.app.ActivityThread.main(ActivityThread.java:5637)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.google.firebase.auth.FirebaseUser.getUid()' on a null object reference
        at com.job.carwash_getfreewashescoupons.ui.MainActivity.onCreate(MainActivity.java:103)
        at android.app.Activity.performCreate(Activity.java:6124)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1112)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2468)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2601) 
        at android.app.ActivityThread.access$800(ActivityThread.java:178) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1470) 
        at android.os.Handler.dispatchMessage(Handler.java:111) 
        at android.os.Looper.loop(Looper.java:194) 
        at android.app.ActivityThread.main(ActivityThread.java:5637) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at java.lang.reflect.Method.invoke(Method.java:372) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 

1 个答案:

答案 0 :(得分:1)

onCreate中的这一行代码假设用户在活动开始时已经登录:

String userId = mAuth.getCurrentUser().getUid();
当没有登录用户时,

getCurrentUser()返回null。您不应该假定活动开始时用户已登录。使用侦听器了解用户何时登录,然后获取其uid。