The error in "adapter = UserAdapter(users)" says type mismatch required: context "Change parameter Context type of primary constructor of class UserAdapter to ArrayList" I tried running it while the "adapter = UserAdapter(users)" is enclosed as a comment (like so //adapter = UserAdapter(users)) in shows an error in logcat saying "java.lang.NullPointerException: v.findViewById(R.id.RVusermain) must not be null
at com.example.chattrialskot.fragments.Year1.onCreateView(Year1.kt:49) ".
USERS.kt
class Users {
var userstuID: String? = null;
var username: String? = null;
var useremail: String? = null;
var useraccstat: String? = null;
var useruid: String? = null;
var useryear: String? = null;
constructor(){}
constructor(userstuID: String?, username: String?, useremail: String?, useraccstat: String?,
useruid: String?, useryear: String?){
this.userstuID = userstuID
this.username = username
this.useremail = useremail
this.useraccstat = useraccstat
this.useruid = useruid
this.useryear = useryear
} }
USER ADAPTER
class UserAdapter(var UAContext: Context, var userlist: ArrayList<Users>):
RecyclerView.Adapter<UserAdapter.UsersViewHolder> (){
fun UserAdapter(userlist: ArrayList<Users>, mContext: Context){
this.userlist = userlist
this.UAContext = mContext
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): UsersViewHolder {
val view:View = LayoutInflater.from(UAContext).inflate(
R.layout.userlayout, parent,
false)
return UserAdapter.UsersViewHolder(view)
}
FRAGMENT
class Year1 : Fragment() {
companion object {
fun newInstance(): Year1 {
return Year1()
}
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
val v = inflater.inflate(R.layout.fragment_year1, container, false)
userrecyclerviewer = v.findViewById<RecyclerView>(R.id.RVyear1)
userrecyclerviewer.setHasFixedSize(true)
userrecyclerviewer.layoutManager = LinearLayoutManager(context)
userrecyclerviewer.adapter = adapter
users = ArrayList()
adapter = UserAdapter(users)
getUsers()
return v
}
private fun getUsers(){
FireAuth = FirebaseAuth.getInstance()
FireDB = FirebaseDatabase.getInstance().getReference()
FireDB.child("users").addValueEventListener(object: ValueEventListener{
override fun onDataChange(snapshot: DataSnapshot) {
users.clear()
for(postSnapshot in snapshot.children){
val userfrmDB = postSnapshot.getValue(Users::class.java)
if(FireAuth.currentUser?.uid != userfrmDB?.useruid
&& userfrmDB?.useraccstat != "Banned")
{
users.add(userfrmDB!!)
}
}
adapter.notifyDataSetChanged()
}
override fun onCancelled(error: DatabaseError) {
}
})
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}}
2
Answers
call getUsers() this method in main thread before you pass the list to adapter because you list should not be null. Then pass the context and list to adapter like this
You were getting Nullpointer exception because you were setting adapter even it has been initialized. Please, follow the code above.