Getting NPE in FirebaseDatabase.getReference()

前端 未结 3 875
抹茶落季
抹茶落季 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条回答
  • 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:

    <service android:name=".MyFirebaseMessagingService">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
    </service>
    

    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'
    }
    
    0 讨论(0)
  • 2021-01-21 00:36

    I downgraded firebase-ui-database from 4.2.0 to 4.1.0 and the problem went away

    0 讨论(0)
  • 2021-01-21 00:40

    add this to the ProGuard configuration, in order to exclude these classes from obfuscation:

    -keep,includedescriptorclasses class com.google.firebase.** { *; }
    

    the fact, that only the release build is being affected hints for this -

    different library versions may provide different (library) consumer rules.

    0 讨论(0)
提交回复
热议问题