最近用kotlin寫一隻聊天APP,寫到聊田內容的時候一直有問題,可以幫忙看一嗎?)
圖:
https://ithelp.ithome.com.tw/upload/images/20210328/201362865AATKYh8cE.png
影片:
更:這是RUN下面紅色的部分,下面寫ChatActivity裡面的34行有錯,可是找不出錯在哪。(小弟我初學者所以沒辦法回復,SORRY)
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.codingwithme.a1234, PID: 8574
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.codingwithme.a1234/com.codingwithme.a1234.acticity.ChatActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: java.lang.NullPointerException
at com.codingwithme.a1234.acticity.ChatActivity.onCreate(ChatActivity.kt:34)
at android.app.Activity.performCreate(Activity.java:6679)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
V/FA: Connection attempt already in progress
錯的是這列
reference = FirebaseDatabase.getInstance.getReference(path: "Users").child(userId!!)
錯誤的說明是
Caused by: java.lang.NullPointerException
at com.codingwithme.a1234.acticity.ChatActivity.onCreate(ChatActivity.kt:34)
簡單說就是程式抓不到資料,NULL 造成閃退
請參考這篇範例
檢查比對看看資料內容是否符合程式去抓的內容