在RecyclerView中遇到onClickListener问题[重复]
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在RecyclerView中遇到onClickListener问题[重复]相关的知识,希望对你有一定的参考价值。
这个问题在这里已有答案:
- how to include different intent in recyclerview 1回答
- Intent fails because of Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName() 2回答
- RecyclerView onClickListener Doesn't Work 2回答
- Intent is not working in my RecyclerView 4个答案
- What is a NullPointerException, and how do I fix it? 12个答案
我只是添加了一个意图,以便我可以在点击回收器视图中存在的项目之一时转到下一页,但是我在那里收到错误,我也点击了添加Toast消息og项目,这工作正常但意图是崩溃的应用程序。而且我在适配器中添加此代码而不是在主活动中。需要一些帮助,谢谢
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
if (holder instanceof ItemViewHolder) {
final Notifications notification = notifications.get(position);
final ItemViewHolder viewHolder = (ItemViewHolder) holder;
viewHolder.title.setText(notifications.get(position).getTitle());
viewHolder.description.setText(notifications.get(position).getDescription());
viewHolder.date.setText(notifications.get(position).getDate());
viewHolder.itemView.setBackgroundColor(Color.GRAY);
viewHolder.notificationLay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText((firebaseMain)activity, "Item clicked " + position, Toast.LENGTH_SHORT).show();
Intent intent = new Intent(firebaseMainActivity, NotificationDetails.class);
context.startActivity(intent);
}
});
} else if (holder instanceof LoadingViewHolder) {
LoadingViewHolder loadingViewHolder = (LoadingViewHolder) holder;
loadingViewHolder.progressBar.setIndeterminate(true);
}
}
E/androidRuntime: FATAL EXCEPTION: main
Process: com.app.sample.collegeautomation, PID: 1875
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
at android.content.ComponentName.<init>(ComponentName.java:130)
at android.content.Intent.<init>(Intent.java:6082)
at com.app.sample.collegeautomation.firebaseNotifications.firebaseListViewAdapter$3.onClick(firebaseListViewAdapter.java:146)
at android.view.View.performClick(View.java:6597)
at com.balysv.materialripple.MaterialRippleLayout$PerformClickEvent.run(MaterialRippleLayout.java:648)
at com.balysv.materialripple.MaterialRippleLayout$3.onAnimationEnd(MaterialRippleLayout.java:336)
at android.animation.Animator$AnimatorListener.onAnimationEnd(Animator.java:552)
at android.animation.AnimatorSet.endAnimation(AnimatorSet.java:1294)
at android.animation.AnimatorSet.doAnimationFrame(AnimatorSet.java:1079)
at android.animation.AnimationHandler.doAnimationFrame(AnimationHandler.java:146)
答案
**create the constrtuctor of the adapter class and pass context**
public UserListAdapter(Context context, int resource,List<UserListDataModel> objects) {
super(context, resource, objects);
this.ctx = context;
}
**now in Intent pass this ctx**
Intent intent = new Intent(ctx,AlarmDispositonActivity.class);
intent.putExtras(bundle);
以上是关于在RecyclerView中遇到onClickListener问题[重复]的主要内容,如果未能解决你的问题,请参考以下文章
如何在我的 recyclerview 中使用 cardview 设置 onclick 侦听器,并在单击 recyclerview 时从 firebase 数据库中检索数据?
从recyclerview中删除项目(viewholder具有onClick但适配器具有数据集)
Firebase UI RecyclerView onClick