0

I have a problem regarding adapter in fragment. It throws a typo shown below.

java.lang.ClassCastException: android.app.Application cannot be cast to androidx.appcompat.app.AppCompatActivity

Here is the code snippet.

SongDetailFragment fragment =
                        SongDetailFragment.newInstance(selectedSong);
                AppCompatActivity activity = (AppCompatActivity) view.getContext().getApplicationContext();
                activity.getSupportFragmentManager().beginTransaction()
                        .replace(R.id.song_detail_container, fragment)
                        .addToBackStack(null)
                        .commit();

How can I fix it?

2
  • 1
    in which class is this code written? Commented Sep 16, 2019 at 18:31
  • did you use this code in your activity? Commented Sep 16, 2019 at 18:31

1 Answer 1

2

In your Fragment use:

AppCompatActivity activity = (AppCompatActivity) getActivity();

instead of:

AppCompatActivity activity = (AppCompatActivity) view.getContext().getApplicationContext();
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.