我正在尝试获取从mySQL数据库接收的数据,并使用onclicklistener在回收器视图中显示它们。回收者视图仅显示标题,当用户单击视图时,会将其带到“详细信息”页面以获取所有其他信息。到目前为止,所有操作均有效,但当我单击查看卡时,应用程序崩溃了。在onclick方法调用上,我一直得到空指针。
E / Android运行时:致命异常:主要 流程:com.example.fit,PID:28226 java.lang.NullPointerException:尝试在空对象引用上调用接口方法'java.lang.Object java.util.List.get(int)' 在com.example.fit.WorkoutLogFragment.onItemClick(WorkoutLogFragment.java:69) 在com.example.fit.WorkoutLogAdapter $ WorkoutLogViewHolder $ 1.onClick(WorkoutLogAdapter.java:59)
这是引发异常的类的代码。
public class WorkoutLogFragment extends Fragment implements WorkoutLogAdapter.OnItemClickListener {
public static final String EXTRA_AUTOMATIC_THOUGHT = "automaticThought";
public static final String EXTRA_DISTORTIONS = "distortions";
public static final String EXTRA_CHALLENGE_THOUGHT = "challengeThought";
public static final String EXTRA_ALTERNATIVE_THOUGHT = "alternativeThought";
private SharedPreferences loginPref;
private RecyclerView recyclerView;
private WorkoutLogAdapter adapter;
private List<WorkoutLogRecycler> workoutLogList;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_workout_log, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
loginPref = getContext().getSharedPreferences("loginPref", Context.MODE_PRIVATE);
int userId = loginPref.getInt("userId", 0);
recyclerView = view.findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.setAdapter(adapter);
Call<List<WorkoutLogRecycler>> call = RetrofitClient.getInstance()
.getApi()
.getLogs(userId);
call.enqueue(new Callback<List<WorkoutLogRecycler>>() {
@Override
public void onResponse(Call<List<WorkoutLogRecycler>> call, Response<List<WorkoutLogRecycler>>response) {
List<WorkoutLogRecycler> workoutLogRecyclers = response.body();
adapter = new WorkoutLogAdapter(getActivity(),workoutLogRecyclers);
recyclerView.setAdapter(adapter);
adapter.setOnItemClickListener(WorkoutLogFragment.this);
}
@Override
public void onFailure(Call<List<WorkoutLogRecycler>> call, Throwable t) {
}
});
}
@Override
public void onItemClick(int position) {
**WorkoutLogRecycler clickedItem = workoutLogList.get(position);**
Fragment workoutLogDetails = new WorkoutLogDetailsFragment();
Bundle args = new Bundle();
args.putString(EXTRA_AUTOMATIC_THOUGHT, clickedItem.getAutomaticThought());
args.putString(EXTRA_CHALLENGE_THOUGHT, clickedItem.getChallengeThought());
args.putString(EXTRA_ALTERNATIVE_THOUGHT, clickedItem.getAlternativeThought());
args.putInt(EXTRA_DISTORTIONS, clickedItem.getDistortions());
workoutLogDetails.setArguments(args);
FragmentTransaction workoutLogTransaction = getActivity().getSupportFragmentManager().beginTransaction();
workoutLogTransaction.replace(R.id.fragment_container, workoutLogDetails);
workoutLogTransaction.commit();
}
}
此行代码根据调试日志触发错误。 WorkoutLogRecycler clickedItem = trainingLogList.get(position);