Home » Android » java – ContextThemeWrapper cannot be cast to AppCompatActivity – Stack Overflow

java – ContextThemeWrapper cannot be cast to AppCompatActivity – Stack Overflow

Posted by: admin February 20, 2020 Leave a comment

Questions:

I want to intent to another fragment but I got an error java.lang.ClassCastException: android.view.ContextThemeWrapper cannot be cast to androidx.appcompat.app.AppCompatActivity.

How to solve that problem if the class extends BottomSheetDialog Fragment?

Cause i have same code intent to another fragment but the class extends Fragment and it’s work.

public class BottomSheetFilter extends BottomSheetDialogFragment {
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.bottom_sheet_filter, container, false);
        baseApiService = RetrofitClient.getClient(UtilsApi.BASE_URL_API).create(BaseApiService.class);
        fetchCategory();
        return view;
    }

    private void fetchCategory() {
        // Get data from JSON
    }
}

and this is my adapter :

public class AdapterFilter extends RecyclerView.Adapter<AdapterFilter.ViewHolder> {
    private Context context;
    private List<ModelCategoryFilter> modelCategoryFilterList;
    private List<ModelSourceFilter> modelSourceFilterList;

    public AdapterFilter(Context context, List<ModelCategoryFilter> modelCategoryFilterList, List<ModelSourceFilter> modelSourceFilterList) {
        this.context = context;
        this.modelCategoryFilterList = modelCategoryFilterList;
        this.modelSourceFilterList = modelSourceFilterList;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_filter, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final ViewHolder holder, int position) {
        final ModelCategoryFilter adapterCategoryName = modelCategoryFilterList.get(position);
        final ModelSourceFilter adapterSourceName = modelSourceFilterList.get(position);

        holder.cardViewSelected.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FilterCategory filterCategory = new FilterCategory();
                Bundle bundle = new Bundle();
                bundle.putInt("categoryID", adapterCategoryName.getId());
                filterCategory.setArguments(bundle);

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