Home Articles

Android Studio - Firebase致命错误

Asked
Viewed 1204 times
-6

有人能告诉我导致错误的原因:

Exception java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.moneyegg.bdpsoft.makemonepaypal/com.moneyegg.bdpsoft.makemonepaypal.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'com.firebase.client.Firebase com.firebase.client.Firebase.child(java.lang.String)' on a null object reference

android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2236)android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)android.app.ActivityThread.access $ 800(ActivityThread.java:151)android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1303)android.os.Handler.dispatchMessage(Handler.java:102)android.os.Looper.loop(Looper.java:135)android.app.ActivityThread.main(ActivityThread.java: 5254)java.lang.reflect.Method.invoke(Method.java)java.lang.reflect.Method.invoke(Method.java:372)com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java: 903)com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

Caused by java.lang.NullPointerException: Attempt to invoke virtual method 'com.firebase.client.Firebase com.firebase.client.Firebase.child(java.lang.String)' on a null object reference

com.moneyegg.bdpsoft.makemonepaypal.MainActivity . (MainActivity.java:35)java.lang.reflect.Constructor.newInstance(Constructor.java)java.lang.Class.newInstance(Class.java:1606)android.app.Instrumentation.newActivity(Instrumentation.java:1066)android .app.ActivityThread.performLaunchActivity(ActivityThread.java:2226)android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)android.app.ActivityThread.access $ 800(ActivityThread.java:151)android.app.ActivityThread $ H. handleMessage(ActivityThread.java:1303)android.os.Handler.dispatchMessage(Handler.java:102)android.os.Looper.loop(Looper.java:135)android.app.ActivityThread.main(ActivityThread.java:5254) java.lang.reflect.Method.invoke(Method.java)java.lang.reflect.Method.invoke(Method.java:372)com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:903) com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

1 Answer

  • 0

    从firebase数据库访问子名称时更改您的声明,看看:

    将您的声明更改为:

    private DatabaseReference databaseRef;
    and assign value to it after you created mRef
    
    mRef =  FirebaseDatabase.getInstance().getReferenceFromUrl("xxxxxxxxxxxxxx");
    databaseRef= mRef.child("Name");
    

    快乐的编码!!

Related