In the fragment I try to access specific classes of my application.

private void initializeAdapter() { Adapter adapter = new Adapter(persons, new Adapter.OnRVItemClickListener(){ @Override public void onRVItemClick(int position) { Intent intent = null; switch(position){ case 0: intent = new Intent(getActivity(), MainActivity.class); break; case 1: intent = new Intent(getActivity(), MainActivity.class); break; default: intent = new Intent(getActivity(), CustomIntro.class); break; } getActivity().startActivity(intent); } }); recyclerView.setAdapter(adapter); } 

Produces the following error:

enter image description here

  • and you pass the context to the method to start with - tCode

1 answer 1

But highlights the red context please tell me how to correct this error?

It is enough to declare and initialize this variable. By the way, this is what Android Studio itself says.

  • yes thanks, added, and how correctly to initialize? - Morozov
  • @VadimMorozov; Differently possible. In this case, you can simply replace context with getActivity() . - post_zeew
  • then an intent in the getActivity (). startActivity (intent); writes that might not have been initialized. - Morozov
  • @VadimMorozov, Well, so you initialize it with something initially. null , for example. This option is also Android Studio itself was supposed to prompt. - post_zeew
  • one
    @VadimMorozov, This is a completely different mistake. But in this case also, Android Studio suggests that your adapter does not have a constructor with the arguments that you pass. Swap them. - post_zeew