Search code examples
androidandroid-recyclerviewdialogadapter

setAdapter crashing application when called from a menu


I am trying to use a dialog box when a menu item is clicked. I can do that successfully when the dialog box doesn't have a recyclerview in it.

Here is the activity code where the menu item click is handled as well as the dialogbox and recyclerview initialization:

  // Menu Stuff
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
      MenuInflater inflater = getMenuInflater();
      inflater.inflate(R.menu.add_exercise_activity_menu, menu);
      return super.onCreateOptionsMenu(menu);
  }

  @Override
  public boolean onOptionsItemSelected(@NonNull MenuItem item) {
      if (item.getItemId() == R.id.timer) {
          Toast bread = Toast.makeText(getApplicationContext(), "Timer Selected", Toast.LENGTH_SHORT);
          bread.show();
      } else if (item.getItemId() == R.id.history) {
          // Prepare to show exercise history dialog box
          LayoutInflater inflater = LayoutInflater.from(AddExerciseActivity.this);
          View view = inflater.inflate(R.layout.exercise_history_dialog, null);
          AlertDialog alertDialog = new AlertDialog.Builder(AddExerciseActivity.this).setView(view).create();

          // Declare local data structure
          ArrayList<WorkoutExercise> All_Performed_Sessions = new ArrayList<>();

          // Find all performed sessions of a specific exercise and add them to local data structure
          for (int i = 0; i < MainActivity.Workout_Days.size(); i++) {
              for (int j = 0; j < MainActivity.Workout_Days.get(i).getExercises().size(); j++) {
                  if (MainActivity.Workout_Days.get(i).getExercises().get(j).getExercise().equals(exercise_name)) {
                      All_Performed_Sessions.add(MainActivity.Workout_Days.get(i).getExercises().get(j));
                  }
              }
          }

          // Set Exercise History Recycler View
          RecyclerView recyclerView = findViewById(R.id.recyclerView_Exercise_History);
          WorkoutExerciseAdapter4 workoutExerciseAdapter4 = new WorkoutExerciseAdapter4(AddExerciseActivity.this, All_Performed_Sessions);

          // Crash Here
          recyclerView.setAdapter(workoutExerciseAdapter4);
          recyclerView.setLayoutManager(new LinearLayoutManager(AddExerciseActivity.this));

          alertDialog.show();
      }
      return super.onOptionsItemSelected(item);
   }

This is the adapter code:

public class WorkoutExerciseAdapter4 extends
    RecyclerView.Adapter<WorkoutExerciseAdapter4.MyViewHolder> {

    Context ct;
    ArrayList<WorkoutExercise> Exercises;

    public WorkoutExerciseAdapter4(Context ct, ArrayList<WorkoutExercise> Exercises) {
        this.ct = ct;
        this.Exercises = new ArrayList<>(Exercises);
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(this.ct);
        View view = inflater.inflate(R.layout.workout_exercise_row4, parent, false);
        return new MyViewHolder(view);

    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        // Change TextView text
        holder.tv_date.setText(Exercises.get(position).getDate());

        // Recycler View Stuff
        // Change RecyclerView items
        WorkoutSetAdapter workoutSetAdapter = new WorkoutSetAdapter(ct, Exercises.get(position).getSets());
        holder.recyclerView.setAdapter(workoutSetAdapter);
        holder.recyclerView.setLayoutManager(new LinearLayoutManager(ct));
    }


    @Override
    public int getItemCount() {
        return this.Exercises.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        TextView tv_date;
        RecyclerView recyclerView;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            tv_date = itemView.findViewById(R.id.tv_date);
            recyclerView = itemView.findViewById(R.id.recycler_view_day);
        }
    }
}

I have tried using getApplicationContext(), this, Activityname.this, but nothing seems to work, and I always get a crash on the same setAdapter(...) line.


Solution

  • Try to change this:

    RecyclerView recyclerView = findViewById(R.id.recyclerView_Exercise_History);
    

    To this:

    RecyclerView recyclerView = view.findViewById(R.id.recyclerView_Exercise_History);