前提・実現したいこと
firestoreに登録したデータを取得し、recyclerviewに表示したい
そのために、発生しているデフォルトコンストラクタのエラーを解決したい
発生している問題・エラーメッセージ
E/AndroidRuntime: FATAL EXCEPTION: main Process: jp.co.stv_tech.test_test, PID: 430 java.lang.RuntimeException: Could not deserialize object. Class com.google.firebase.firestore.auth.User does not define a no-argument constructor. If you are using ProGuard, make sure these constructors are not stripped at com.google.firebase.firestore.util.CustomClassMapper.deserializeError(com.google.firebase:firebase-firestore@@21.4.3:563) at com.google.firebase.firestore.util.CustomClassMapper.access$200(com.google.firebase:firebase-firestore@@21.4.3:54) at com.google.firebase.firestore.util.CustomClassMapper$BeanMapper.deserialize(com.google.firebase:firebase-firestore@@21.4.3:749) at com.google.firebase.firestore.util.CustomClassMapper$BeanMapper.deserialize(com.google.firebase:firebase-firestore@@21.4.3:741) at com.google.firebase.firestore.util.CustomClassMapper.convertBean(com.google.firebase:firebase-firestore@@21.4.3:542) at com.google.firebase.firestore.util.CustomClassMapper.deserializeToClass(com.google.firebase:firebase-firestore@@21.4.3:253) at com.google.firebase.firestore.util.CustomClassMapper.convertToCustomClass(com.google.firebase:firebase-firestore@@21.4.3:100) at com.google.firebase.firestore.DocumentSnapshot.toObject(com.google.firebase:firebase-firestore@@21.4.3:187) at com.google.firebase.firestore.QueryDocumentSnapshot.toObject(com.google.firebase:firebase-firestore@@21.4.3:116) at com.google.firebase.firestore.DocumentSnapshot.toObject(com.google.firebase:firebase-firestore@@21.4.3:165) at com.google.firebase.firestore.QueryDocumentSnapshot.toObject(com.google.firebase:firebase-firestore@@21.4.3:97) at com.firebase.ui.firestore.ClassSnapshotParser.parseSnapshot(ClassSnapshotParser.java:23) at com.firebase.ui.firestore.ClassSnapshotParser.parseSnapshot(ClassSnapshotParser.java:12) at com.firebase.ui.common.BaseCachingSnapshotParser.parseSnapshot(BaseCachingSnapshotParser.java:36) at com.firebase.ui.common.BaseObservableSnapshotArray.get(BaseObservableSnapshotArray.java:52) at com.firebase.ui.firestore.FirestoreRecyclerAdapter.getItem(FirestoreRecyclerAdapter.java:86) at com.firebase.ui.firestore.FirestoreRecyclerAdapter.onBindViewHolder(FirestoreRecyclerAdapter.java:158) at androidx.recyclerview.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6781) at androidx.recyclerview.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6823) at androidx.recyclerview.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5752) at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6019) at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5858) at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5854) at androidx.recyclerview.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2230) at androidx.recyclerview.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1557) at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1517) at androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:612) at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3924) at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:3641) at androidx.recyclerview.widget.RecyclerView.onLayout(RecyclerView.java:4194) at android.view.View.layout(View.java:18804) at android.view.ViewGroup.layout(ViewGroup.java:5951) at androidx.constraintlayout.widget.ConstraintLayout.onLayout(ConstraintLayout.java:1915) at android.view.View.layout(View.java:18804) at android.view.ViewGroup.layout(ViewGroup.java:5951) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323) at android.widget.FrameLayout.onLayout(FrameLayout.java:261) at android.view.View.layout(View.java:18804) at android.view.ViewGroup.layout(ViewGroup.java:5951) E/AndroidRuntime: at androidx.appcompat.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:446)
該当のソースコード
import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.util.Log import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.TextView import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView import com.firebase.ui.firestore.FirestoreRecyclerAdapter import com.firebase.ui.firestore.FirestoreRecyclerOptions import com.google.firebase.firestore.FirebaseFirestore import com.google.firebase.firestore.Query import com.google.firebase.firestore.auth.User import kotlinx.android.synthetic.main.activity_main.* private val lastName: Unit get() = Unit private val firstName: Unit get() = Unit class MainActivity(private val TAG: String = "MyMessage") : AppCompatActivity() { private lateinit var mAdapter: UserFirestoreRecyclerAdapter override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val db = FirebaseFirestore.getInstance() val query = db.collection("users").orderBy("firstName", Query.Direction.ASCENDING) val options = FirestoreRecyclerOptions.Builder<User>().setQuery(query, User::class.java).build() add_button.setOnClickListener { val firstName = firstname_edit.text.toString() val lastName = lastname_edit.text.toString() val user = User(firstName, lastName) db.collection("users") .add(user) .addOnSuccessListener { documentReference -> Log.d(TAG, "DocumentSnapshot added with ID: ${documentReference.id}") } .addOnFailureListener { e -> Log.w(TAG, "Error adding document", e) } } val recyclerView = findViewById<RecyclerView>(R.id.recyclerview) mAdapter = UserFirestoreRecyclerAdapter(options) recyclerView.layoutManager = LinearLayoutManager(this) recyclerView.adapter = mAdapter } private inner class UserViewHolder internal constructor(private val view: View) : RecyclerView.ViewHolder(view){ internal fun setUserName(firstName: Unit, lastName: Unit){ val textviewFirst = view.findViewById<TextView>(R.id.text_first) textviewFirst.text = firstName.toString() val textviewLast = view.findViewById<TextView>(R.id.text_last) textviewLast.text = lastName.toString() } } private inner class UserFirestoreRecyclerAdapter internal constructor(options: FirestoreRecyclerOptions<User>): FirestoreRecyclerAdapter<User, UserViewHolder>(options){ override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): UserViewHolder { val view = LayoutInflater.from(parent.context).inflate(R.layout.raw,parent, false) return UserViewHolder(view) } override fun onBindViewHolder(holder: UserViewHolder, position: Int, model: User) { holder.setUserName(firstName, lastName) } } override fun onStart() { super.onStart() mAdapter.startListening() } override fun onStop() { super.onStop() mAdapter.stopListening() } }
data class User( val firstName: String = "", val lastName: String = "" )
試したこと
調べてみると、デフォルトコンストラクタが必要であり、
デフォルトコンストラクタを作るにはフィールドにデフォルト値を入れる必要があるとの記載があったので
Userクラスにデフォルト値を入れました。
val user = User(firstName, lastName)
私はこちらの1行がデフォルトコはンストラクタであるのかなと考えており、エラーの原因がわかっておりません。
デフォルトコンストラクタについての考え方をご教示いただけると幸いです。
あなたの回答
tips
プレビュー