从来好事天生俭,自古瓜儿苦后甜。这篇文章主要讲述我在我的android应用程序类中有错误我正在检查auth!= null但仍然在if if语句中?相关的知识,希望能为你提供帮助。
【我在我的android应用程序类中有错误我正在检查auth!= null但仍然在if if语句中()】当我从应用程序注销并尝试登录时,我的应用程序类如下所示
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 .FaultbaseAuth.getCurrentUser()'在android.app.ActivityThread.access上的空对象引用(ActivityThread.java:4999),在android.app.ActivityThread $ android.app.ActivityThread.access $ 1600(ActivityThread.java:188) 2.handleMessage(ActivityThread.java:1591)位于android.app.A.Toread.main(androidT.read)的android.os.Handler.dispatchMessage(Handler.java:111)android.app.Looper.loop(Looper.java:210) .java:5839)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:1113)com.android.internal.os.ZygoteInit.main(ZygoteInit.java:879)引起:java.lang.NullPointerException:尝试调用虚方法'com .google.firebase.auth.FirebaseUser com.google.firebase.auth.FirebaseAuth.getCurrentUser()'在com.example.crowderia.whatsapp.Service.WhatsApp.onCreate(WhatsApp.java:39)的空对象引用上.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1017)答案
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if (user != null) {} else {
goLogInScreen();
}
试试那个实例
FirebaseAuth.getInstance() != null
推荐阅读
- android上的nullpointer异常[重复]
- Android studio错误(java.lang.NullPointerException [重复])
- 尝试调用虚方法'void android.view.View.setOnClickListener [duplicate]
- 在android [duplicate]中运行时检查权限时获取NullPointerException
- safari app extensions(从swift后台进程向所有选项卡广播消息)
- Android findViewById空指针[重复]
- 从Safari打开相机APP
- Java applet无法在Safari 7下打开文件(Mac OS X 10.9)
- Safari Webapp强制在单击其他链接时打开Safari