我在我的android应用程序类中有错误我正在检查auth!= null但是它仍然在if语句中?

时间:2017-12-31 08:01:53

标签: android firebase nullpointerexception firebase-authentication

当我从应用程序注销并尝试登录时,我的应用程序类如下所示

public class WhatsApp extends Application {

        private DatabaseReference userReference;
        private FirebaseAuth auth;

        @Override
        public void onCreate() {
            super.onCreate();

            FirebaseDatabase.getInstance().setPersistenceEnabled(true);

            Picasso.Builder builder = new Picasso.Builder(this);
            builder.downloader(new OkHttpDownloader(this, Integer.MAX_VALUE));
            Picasso built = builder.build();
            built.setIndicatorsEnabled(true);
            built.setLoggingEnabled(true);
            Picasso.setSingletonInstance(built);

            if(FirebaseAuth.getInstance() != null) {

                userReference = FirebaseDatabase.getInstance().getReference().child("Users").child(auth.getCurrentUser().getUid());

                userReference.addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        if(dataSnapshot != null) {
                            userReference.child("online").onDisconnect().setValue(false);
                        }
                    }
                    @Override
                    public void onCancelled(DatabaseError databaseError) {

                    }
                });

            } else {

            }
        }
    }

我收到如下错误

  

java.lang.RuntimeException:无法创建应用程序   com.example.crowderia.whatsapp.Service.WhatsApp:   java.lang.NullPointerException:尝试调用虚方法   “com.google.firebase.auth.FirebaseUser   com.google.firebase.auth.FirebaseAuth.getCurrentUser()'上的null   对象参考                                                                                       在   android.app.ActivityThread.handleBindApplication(ActivityThread.java:4999)                                                                                       在android.app.ActivityThread.access $ 1600(ActivityThread.java:188)                                                                                       在   android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1591)                                                                                       在android.os.Handler.dispatchMessage(Handler.java:111)                                                                                       在android.os.Looper.loop(Looper.java:210)                                                                                       在android.app.ActivityThread.main(ActivityThread.java:5839)                                                                                       at java.lang.reflect.Method.invoke(Native Method)                                                                                       在java.lang.reflect.Method.invoke(Method.java:372)                                                                                       在   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1113)                                                                                       在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:879)                                                                                    引起:java.lang.NullPointerException:尝试调用虚拟   方法'com.google.firebase.auth.FirebaseUser   com.google.firebase.auth.FirebaseAuth.getCurrentUser()'上的null   对象参考                                                                                       在   com.example.crowderia.whatsapp.Service.WhatsApp.onCreate(WhatsApp.java:39)                                                                                       在   android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1017)

1 个答案:

答案 0 :(得分:0)

FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if (user != null) {    

} else {
    goLogInScreen();
}

尝试

的那个实例
FirebaseAuth.getInstance() != null