Getting NPE in FirebaseDatabase.getReference()

前端 未结 3 881
抹茶落季
抹茶落季 2021-01-21 00:13

I am getting NPE in production build on app start and only once after reinstalling with adb.

Caused by java.lang.NullPointerException
Attempt to invoke interfac         


        
3条回答
  •  旧时难觅i
    2021-01-21 00:15

    I found solution that actually work's for me in this link

    The FirebaseMessagingService is causing me the issue. So, to reproduce, add:

    Manifest:

    
            
                
            
    
    

    MyFirebaseMessagingService.class

    public class MyFirebaseMessagingService extends FirebaseMessagingService {
    
    @Override
    public void onNewToken(String token) {
    
        try{
            DatabaseReference fRef = FirebaseDatabase.getInstance().getReference();
            //  I am getting the reference to write the token in firebase database like:
            // fRef.child("token").setValue(token);
        } catch (Exception e) {
            Log.d("MyFirebaseMsgService", e.getStackTrace().toString());
        }
    }
    
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
    
    }
    }
    

    In firebase-database 16.0.2, the line DatabaseReference fRef = FirebaseDatabase.getInstance().getReference(); is firing an exception:

    Can't create handler inside thread that has not called Looper.prepare()

    whereas in 16.0.1 it wasn't. When this exception is fired, the next firebaseDatabase.getReference() is crashing with the exception described initially.

    Dependencies:

    dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    
    implementation 'com.android.support:support-v4:27.1.1'
    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    implementation 'com.android.support:design:27.1.1'
    implementation 'com.google.firebase:firebase-core:16.0.3'
    implementation 'com.google.firebase:firebase-messaging:17.3.1'
    implementation 'com.google.firebase:firebase-invites:16.0.3'
    implementation 'com.firebaseui:firebase-ui-auth:4.2.0'
    
    implementation 'com.firebaseui:firebase-ui-database:4.2.0'
    //    implementation('com.firebaseui:firebase-ui-database:4.2.0') {
    //        exclude group: 'com.google.firebase', module: 'firebase-database'
    //    }
    //    implementation 'com.google.firebase:firebase-database:16.0.1'
    }
    

提交回复
热议问题