java – ContextThemeWrapper cannot be cast to AppCompatActivity-ThrowExceptions

Exception or error:

I have BottomSheetDialogFragment and I want to move to another fragment when i click relativeAZ but I got an error java.lang.ClassCastException: android.view.ContextThemeWrapper cannot be cast to androidx.appcompat.app.AppCompatActivity.

This is my BottomSheet xml :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="15dp">

    <RelativeLayout
        android:id="@+id/relativeAZ"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="20dp">

        <TextView
            android:id="@+id/filterAZ"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="A - Z"
            android:textColor="@color/Black"
            android:textSize="18sp" />

    </RelativeLayout>

</LinearLayout>

and this is my BottomSheet class :

public class BottomUrutkanCategory extends BottomSheetDialogFragment {
    private RelativeLayout filterAZ;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.bottom_sheet_urutkan, container, false);
        filterAZ = view.findViewById(R.id.relativeAZ);

        filterAZ.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MenuHomeUrutkanNameAscendings menuHomeUrutkan = new MenuHomeUrutkanNameAscendings();

                AppCompatActivity activity = (AppCompatActivity) v.getContext();
             activity.getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, menuHomeUrutkan).addToBackStack(null).commit();
            }
        });

        return view;
    }
}
How to solve:

Leave a Reply

Your email address will not be published. Required fields are marked *