Home > Blockchain >  Issue with the BottomSheetDialogFragment in - Android
Issue with the BottomSheetDialogFragment in - Android

Time:07-21

I was just upgrading the old android project and everything I have done properly and at the last I am just getting below error :

   2022-07-21 15:19:06.548 19455-19455/com.brian.skyazul E/USNET: USNET: appName: com.packagename
2022-07-21 15:19:48.290 19455-19455/com.brian.skyazul E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.packagename, PID: 19455
    java.lang.IllegalStateException: Fragment com.brian.skyazul.fragment.ManualsFragment.BottomSheetFilterDialog must be a public static class to be  properly recreated from instance state.
        at androidx.fragment.app.FragmentTransaction.doAddOp(FragmentTransaction.java:249)
        at androidx.fragment.app.BackStackRecord.doAddOp(BackStackRecord.java:183)
        at androidx.fragment.app.FragmentTransaction.add(FragmentTransaction.java:171)
        at androidx.fragment.app.DialogFragment.show(DialogFragment.java:259)
        at com.brian.skyazul.fragment.ManualsFragment$1.onClick(ManualsFragment.java:80)
        at android.view.View.performClick(View.java:7792)
        at android.view.View.performClickInternal(View.java:7769)
        at android.view.View.access$3800(View.java:910)
        at android.view.View$PerformClick.run(View.java:30218)
        at android.os.Handler.handleCallback(Handler.java:938)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loopOnce(Looper.java:226)
        at android.os.Looper.loop(Looper.java:313)
        at android.app.ActivityThread.main(ActivityThread.java:8663)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:567)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1135)

There is a code as below for BottomSheetFragment :

mFilterDialog = new BottomSheetFilterDialog();
mFilterDialog.show(getActivity().getSupportFragmentManager(), mFilterDialog.getTag());

Error is pointing at below line :

mFilterDialog.show(getActivity().getSupportFragmentManager(), mFilterDialog.getTag());

More Implementation is here :

@SuppressLint("ValidFragment")
public class BottomSheetFilterDialog extends BottomSheetDialogFragment {
    private RecyclerView mRecFilter;
    private Button btnClear;
    private FilterAdapter mFilterAdapter;
    @SuppressLint("RestrictedApi")
    @Override
    public void setupDialog(final Dialog dialog, int style) {
        super.setupDialog(dialog, style);
        View contentView = View.inflate(getContext(), R.layout.fragment_bottom_sheet_filters_option, null);
        dialog.setContentView(contentView);
        mRecFilter = (RecyclerView) contentView.findViewById(R.id.rec_filter);
        btnClear = (Button) contentView.findViewById(R.id.fragment_bottom_sheet_btn_clear);
        mFilterAdapter = new FilterAdapter();
        RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getActivity());
        mRecFilter.addItemDecoration(new DividerDecoration(getActivity()));
        mRecFilter.setLayoutManager(mLayoutManager);
        mRecFilter.setItemAnimator(new DefaultItemAnimator());
        mFilterAdapter.addAll(lstManualHeaderCategoryName);
        mRecFilter.setAdapter(mFilterAdapter);

        btnClear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mCategoryAdapter.clear();
                mCategoryAdapter.addAll(model);
                getDialog().dismiss();
            }
        });

        mRecFilter.addOnItemTouchListener(
                new CommonRecyclerAdapter(new OnItemClickListener() {
                    @Override
                    public void onItemClick(View view, int position) {
                        mCategoryAdapter.clear();
                        String keyName = null;
                        try {
                            keyName = namearray.get(position).toString();
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        filterModel = new ArrayList<ManualViewTypeModel>();
                            try {
                                JSONObject jsonObject = (JSONObject) (mFilterAdapter.getItem(position));
                                ManualViewTypeModel modelHeader = new ManualViewTypeModel();
                                modelHeader.setCategoryId(jsonObject.getString("category_id"));
                                modelHeader.setId(namearray.get(position).toString());
                                modelHeader.setCategoryName(jsonObject.getString("category_name"));
                                modelHeader.setRowViewItemtype(ManualViewTypeModel.HEADER);
                                modelHeader.setCategoryImage(jsonObject.getString("category_image"));
                                filterModel.add(modelHeader);
                                for (int j = 0; j < jsonObject.getJSONArray("manuals").length(); j  ) {
                                    ManualViewTypeModel modelManual = new ManualViewTypeModel();
                                    modelManual.setCategoryId(jsonObject.getString("category_id"));
                                    modelManual.setId(namearray.get(position).toString());
                                    modelManual.setCategoryName(jsonObject.getString("category_name"));
                                    modelManual.setRowViewItemtype(ManualViewTypeModel.ROWITEM);
                                    Manual manual = new Manual();
                                    manual.setManualFile(jsonObject.getJSONArray("manuals").getJSONObject(j).getString("manual_file"));
                                    manual.setManualName(jsonObject.getJSONArray("manuals").getJSONObject(j).getString("manual_name"));
                                    modelManual.setManualList(manual);
                                    filterModel.add(modelManual);
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        mCategoryAdapter.addAll(filterModel);
                        getDialog().dismiss();

                    }
                }) {
                    @Override
                    protected int getViewIdToSwitch() {
                        return 0;
                    }
                }
        );
    }
}

What might causing the issue? Thanks.

CodePudding user response:

As of this answer fragments have some conditions so that os could recreate them. One of them is that your fragment should not be an inner class. Because this makes your fragment creation dependent on its outer class.

  • Related