Kotlin / Firebase / Android Studio- java.util.NoSuchElementException: List is empty

Ren*_*lic 2 android kotlin firebase firebase-realtime-database

我制作了一个像 Instagram 这样的应用程序并且一切正常,除非当新用户创建个人资料时,应用程序停止/崩溃,因为它在主页上没有任何显示,并且它会丢弃 `java.util.NoSuchElementException: List is empty .

这是完整的错误

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.homeactivity, PID: 14508
java.util.NoSuchElementException: List is empty.
    at kotlin.collections.CollectionsKt___CollectionsKt.first(_Collections.kt:196)
    at com.example.homeactivity.activities.MainActivity$onStart$1.invoke(MainActivity.kt:61)
    at com.example.homeactivity.activities.MainActivity$onStart$1.invoke(MainActivity.kt:30)
    at com.example.homeactivity.utils.ValueEventListenerAdapter.onDataChange(ValueEventListenerAdapter.kt:13)
    at com.google.firebase.database.core.ValueEventRegistration.fireEvent(com.google.firebase:firebase-database@@19.2.0:75)
    at com.google.firebase.database.core.view.DataEvent.fire(com.google.firebase:firebase-database@@19.2.0:63)
    at com.google.firebase.database.core.view.EventRaiser$1.run(com.google.firebase:firebase-database@@19.2.0:55)
    at android.os.Handler.handleCallback(Handler.java:751)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    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)
Run Code Online (Sandbox Code Playgroud)

这是 MainActivity.kt 第 51-66 行

override fun onStart() {
    super.onStart()
    val currentUser = mFirebase.auth.currentUser
    if(currentUser == null){
        startActivity(Intent(this, LoginActivity::class.java))
        finish()
    } else {
        mFirebase.database.child("feed-posts").child(currentUser.uid)
            .addValueEventListener(ValueEventListenerAdapter { it ->
                val posts = it.children.map { it.getValue(FeedPost::class.java)!! }
                Log.d(TAG, "feedPosts: ${posts.first().timestampDate()}")
                feed_recycler.adapter = FeedAdapter(posts)
                feed_recycler.layoutManager = LinearLayoutManager(this)
            })
    }
}
Run Code Online (Sandbox Code Playgroud)

MainActivity.kt 第 30-32 行

class MainActivity : BaseActivity(0) {
private val TAG = "MainActivity"
private lateinit var mFirebase: FirebaseHelper
Run Code Online (Sandbox Code Playgroud)

ValueEventListenerAdapter.kt 第 8-19 行

    class ValueEventListenerAdapter(val handler: (DataSnapshot) -> Unit): ValueEventListener {
        private val TAG = "ValueEventListenerAdapt"

        override fun onDataChange(data: DataSnapshot) {
           handler(data)
        }

        override fun onCancelled(error: DatabaseError) {
           Log.e(TAG, "onCancelled: ", error.toException())
        }
    }
Run Code Online (Sandbox Code Playgroud)

Md.*_*man 10

您尝试first从空列表中获取元素。尝试删除这个

//Log.d(TAG, "feedPosts: ${posts.first().timestampDate()}")
Run Code Online (Sandbox Code Playgroud)

而是尝试使用firstOrNull如下所示:

Log.d(TAG, "feedPosts: ${posts.firstOrNull()?.timestampDate()}")
Run Code Online (Sandbox Code Playgroud)