提问人:Andrey 提问时间:9/14/2022 更新时间:9/15/2022 访问量:958
尝试调用虚拟方法...在 null 对象引用上
Attempt to Invoke virtual method ... on a null object reference
问:
每次尝试从 Firebase Database 过滤数据并将它们添加到我的 .RecyclerView
如果我传递一切正常,我想要的所有项目都将被添加到 中,但如果我传递它向我显示一个空对象引用异常。when(id) { "" -> {...}}
RecyclerView
when(id) { else -> {...}}
- 异常在代码下方 -
fun fetchVideos(id: String?) {
if (id == currentFilter) {
currentFilter = null
return fetchSubs()
}
currentFilter = id
val uid = auth.uid
val ref = database.getReference("/content/videos")
ref.addListenerForSingleValueEvent(object: ValueEventListener {
@SuppressLint("NotifyDataSetChanged")
override fun onDataChange(p0: DataSnapshot) {
val adapter = GroupAdapter<GroupieViewHolder>()
p0.children.forEach {
val video = it.getValue(Video::class.java)
when(id) {
"" -> {
if (video != null && video.publisher_uid != uid) {
val user = database.getReference("users/$uid/subscriptions/${video.publisher_uid}")
user.addListenerForSingleValueEvent(object : ValueEventListener{
override fun onDataChange(p0: DataSnapshot) {
val sub = p0.getValue(Subscription::class.java) ?: return
if (sub.subscribed == true){
adapter.add(VideoItem(video))
}
}
override fun onCancelled(error: DatabaseError) {
}
})
}
}
else -> {
if (video != null && video.publisher_uid == id) {
adapter.add(VideoItem(video))
}
}
}
}
// New Bottom Sheet
adapter.setOnItemClickListener{ item, view ->
val videoItem = item as VideoItem
selectedVideo = videoItem.video.video_uid
HomeFragment.selectedVideo = selectedVideo
updateViewCount()
addRecentVideo()
showVideoPlayer()
createValues()
}
try {
sub_recyclerview_videos.adapter = adapter
}catch (e: Exception){
Log.d(TAG,"${e.message}")
}
adapter.notifyDataSetChanged()
}
override fun onCancelled(p0: DatabaseError) {
}
})
}
这是我的例外:
尝试在空对象引用上调用虚拟方法“void androidx.recyclerview.widget.RecyclerView.setAdapter(androidx.recyclerview.widget.RecyclerView$Adapter)”
答:
0赞
Andrey
9/15/2022
#1
问题不在于适配器或我的布局,实际上是我的类。SubItem
class SubItem(val channel: Channel) : Item<GroupieViewHolder>() {
@SuppressLint("ResourceAsColor")
override fun bind(viewHolder: GroupieViewHolder, position: Int) {
var selected_position = SubscriptionsFragment.selected_position
val profileImageUrl = channel.channel_logo
viewHolder.itemView.sub_item_name.text = channel.channel_name
viewHolder.itemView.sub_item_layout.setBackgroundResource(R.color.white)
viewHolder.itemView.sub_item_name.setTextColor(R.color.colorSecondaryText)
val targetImageView = viewHolder.itemView.sub_item_profile
try {
Picasso.get().load(profileImageUrl)
.placeholder(R.drawable.ic_baseline_account_circle_24)
.into(targetImageView)
}catch (e:Exception){
Log.d("SubItem","${e.message}")
// Toast.makeText(,e.message,Toast.LENGTH_SHORT).show()
}
/*viewHolder.itemView.sub_item_layout.setOnClickListener {
selected_position = position
// because I am calling here it doesn't know my recyclerview
SubscriptionsFragment().fetchVideos(channel.uid) // because I am calling here fetchVideos it blocked another clickListener for the same object
notifyChanged()
}*/
checkFilter(viewHolder,position)
if (SubscriptionsFragment.list[position]){
viewHolder.itemView.sub_item_layout.setBackgroundResource(R.color.colorSecondaryText)
viewHolder.itemView.sub_item_name.setTextColor(R.color.black)
}
else{
viewHolder.itemView.sub_item_layout.setBackgroundResource(R.color.white)
viewHolder.itemView.sub_item_name.setTextColor(R.color.colorSecondaryText)
}
}
评论
sub_recyclerview_videos
setAdapter()
sub_recyclerview_videos
fragment_subscriptions.xml
SubscriptionsFragment
fetchVideos
return inflater.inflate(R.layout.fragment_subscriptions, container, false)