-1

I just added an intent so that i can move on to the next page upon clicking on one of the item which is present in the recycler view, but i'm getting an error over there, also i have added a Toast message upon the click og the item and that is working fine but the intent is crashing the app. And also i'm adding this code in the adapter not in the main activity. Need some help, thanks

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)
Beyazid
  • 1,795
  • 1
  • 15
  • 28
Niroop N
  • 156
  • 1
  • 9
  • `firebaseMainActivity` is null. – Mike M. Apr 24 '19 at 07:39
  • i do have initialized it to the main java file at first itself – Niroop N Apr 24 '19 at 07:41
  • which line is the 146th line? – Beyazid Apr 24 '19 at 07:43
  • Why you are using 3 different Context Variable in 3 lines ? This is way too much confusing ? Probably `firebaseMainActivity` is null here.. How did you initialized all these Context add the code .. – ADM Apr 24 '19 at 07:43
  • I'm not sure what you're saying, but if that's where it's crashing, `firebaseMainActivity` is null there. – Mike M. Apr 24 '19 at 07:43
  • the 146th line is where i have put the intent – Niroop N Apr 24 '19 at 07:51
  • 1
    If you want to know why `firebaseMainActivity` and `context` are null, you'll need to provide more information. Otherwise, just use `activity` in place of both of those, since it's apparently not null, as it's not crashing on the `Toast`. – Mike M. Apr 24 '19 at 07:57

1 Answers1

1
**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);
mDeveloper
  • 122
  • 9