从菜单调用时 setAdapter 崩溃应用程序

setAdapter crashing application when called from a menu

我正在尝试在单击菜单项时使用对话框。当对话框中没有 recyclerview 时,我可以成功地做到这一点。

这是 activity 处理菜单项点击以及对话框和 recyclerview 初始化的代码:

  // 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);
   }

这是适配器代码:

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);
        }
    }
}

我尝试过使用 getApplicationContext()thisActivityname.this,但似乎没有任何效果,而且我总是在同一行 setAdapter(...) 上崩溃。

尝试改变这个:

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

为此:

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