Cześć, próbuje w recyclerView wyświetlić liste użytkowników ale mam problem. Tutaj wyskakuje mi błąd userRecyclerView.layoutManager = LinearLayoutManager(this.context)
Process: com.android.firebasesocialmediaapp, PID: 17338
java.lang.IllegalStateException: userRecyclerView must not be null
at com.android.firebasesocialmediaapp.Fragment.UsersFragment.onCreateView(UsersFragment.kt:30)
private val userList: ArrayList<ModelUser> = ArrayList()
class UsersFragment : Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
// Inflate the layout for this fragment
val view = inflater.inflate(R.layout.fragment_profile, container, false)
userRecyclerView.layoutManager = LinearLayoutManager(this.context)
userRecyclerView.setHasFixedSize(true)
//get all users
getAllUsers()
return view
}
private fun getAllUsers(){
//get current users
val user = FirebaseAuth.getInstance().currentUser
val ref = FirebaseDatabase.getInstance().getReference("Users")
//get all data from path
ref.addValueEventListener(object : ValueEventListener{
override fun onDataChange(snapshot: DataSnapshot) {
userList.clear()
for(item in snapshot.children){
val modelUser: ModelUser = item.value as ModelUser
if(modelUser.uid != user!!.uid){
userList.add(modelUser)
}
userRecyclerView.adapter = UserListAdapter(userList, activity)
userRecyclerView.adapter?.notifyDataSetChanged()
}
}
override fun onCancelled(error: DatabaseError) {}
})
}
}
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Fragment.UsersFragment">
<android.support.v7.widget.RecyclerView
android:id="@+id/userRecyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v7.widget.RecyclerView>
</FrameLayout>
class MyViewHolder(view: View): RecyclerView.ViewHolder(view){
val mNameTv : TextView? = view.findViewById(R.id.nameTv)
val mEmailTv : TextView? = view.findViewById(R.id.emailTv)
val mAvatarIv : ImageView? = view.findViewById(R.id.avatarTv)
}
class UserListAdapter(private var userList: ArrayList<ModelUser>, private val context: FragmentActivity?): RecyclerView.Adapter<MyViewHolder>(){
override fun onCreateViewHolder(viewGroup: ViewGroup, position: Int): MyViewHolder {
val itemView = LayoutInflater.from(viewGroup.context).inflate(R.layout.row_user, viewGroup, false)
return MyViewHolder(itemView)
}
override fun getItemCount(): Int = userList.count()
override fun onBindViewHolder(viewHolder: MyViewHolder, position: Int) {
//get data
val userImage = userList[position].image
val userName = userList[position].name
val userEmail = userList[position].email
//set data
viewHolder.mNameTv!!.text = userName
viewHolder.mEmailTv!!.text = userEmail
try {
Glide.with(this!!.context!!).load(userImage).placeholder(R.drawable.ic_face).into(viewHolder.mAvatarIv!!)
}catch (e: Exception){
Toast.makeText(context, ""+e.message, Toast.LENGTH_SHORT).show()
}
}
}
Próbowałem już chyba wszystkiego. Nic nie działa 🙂 Przedtem nie miałem zadnych problem z recyclerView, a teraz coś nie gra ?