DialogFragment при вызове выдает ошибку

При нажатии на LinearLayout должен открываться DialogFragment в котором находится RadioGroup с помощью которого я смогу сортировать компоненты в RecyclerView, но сейчас класс не static и при нажатии приложение крашиться с ошибкой:

  java.lang.IllegalStateException: Fragment com.example.testfuntura.Attraction.FragmentAttractionRecyclerView.DialogSort 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:180)
        at androidx.fragment.app.FragmentTransaction.add(FragmentTransaction.java:171)
        at androidx.fragment.app.DialogFragment.show(DialogFragment.java:180)
        at com.example.testfuntura.Attraction.FragmentAttractionRecyclerView.lambda$onViewCreated$0$FragmentAttractionRecyclerView(FragmentAttractionRecyclerView.java:76)
        at com.example.testfuntura.Attraction.-$$Lambda$FragmentAttractionRecyclerView$ialawKfmCyFP4rmQYxEQbEeK6b4.onClick(Unknown Source:2)
        at android.view.View.performClick(View.java:7448)
        at android.view.View.performClickInternal(View.java:7425)

Но если класс сделать public static class то у меня нет возможности ссылаться на: private AdapterAttractions adapter; и private ArrayList<ItemAttractions> exampleList;

Как сделать так чтобы приложение не вылетало при нажатии на LinerLayout открывался мой DialogFragment и функционал RadioGroup не пропадал?

Мой DialogFragment

 public static class DialogSort extends DialogFragment {
        RadioGroup rg;
        RadioButton rb1, rb2, rb3;

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

            rg = view.findViewById(R.id.RG);
            rb1 = view.findViewById(R.id.RB_1);
            rb2 = view.findViewById(R.id.RB_2);
            rb3 = view.findViewById(R.id.RB_3);

            rb1.setOnClickListener(v -> {
                {
                    sortArray_alphabet();
                    sortArrayList_alphabet();
                }
            });
            rb2.setOnClickListener(v -> {
                {
                    sortArray_type();
                    sortArrayList_type();
                }
            });
            rb3.setOnClickListener(v -> {
                {
                    sortArray_cost();
                    sortArrayList_cost();
                }
            });
            return view;
        }

        private void sortArray_alphabet() {
            Arrays.sort(mArraysNames);
            adapter.notifyDataSetChanged();
        }

        private void sortArrayList_alphabet() {
            Collections.sort(exampleList, (o1, o2) -> o1.get_attraction_name().compareTo(o2.get_attraction_name()));
            adapter.notifyDataSetChanged();
        }

        private void sortArray_type() {
            Arrays.sort(mArraysNames);
            adapter.notifyDataSetChanged();
        }

        private void sortArrayList_type() {
            Collections.sort(exampleList, (o1, o2) -> o1.get_attraction_type().compareTo(o2.get_attraction_type()));
            adapter.notifyDataSetChanged();
        }

        private void sortArray_cost() {
            Arrays.sort(mArraysNames);
            adapter.notifyDataSetChanged();
        }

        private void sortArrayList_cost() {
            Collections.sort(exampleList, (o1, o2) -> o1.get_attraction_cost().compareTo(o2.get_attraction_cost()));
            adapter.notifyDataSetChanged();
        }
    }

Fragment в котором находиться RecyclerView и LinerLayout по нажатию на который вызывается DialogFragment

public class FragmentAttractionRecyclerView extends Fragment {
    private static final String TAG = "FragmentAttractionRecyclerView";
    private RecyclerView mRec;
    private AdapterAttractions adapter;
    private ArrayList<ItemAttractions> exampleList;
    private RecyclerView.LayoutManager mLayoutManager;
    public final String[] mArraysNames = new String[]{"Baby островок", "Виражи", "Вокруг света", "5D кинотеатр"};

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRetainInstance(true);
        OnBackPressedCallback callback = new OnBackPressedCallback(true) {
            @Override
            public void handleOnBackPressed() {
                Intent homeIntent = new Intent(Intent.ACTION_MAIN);
                homeIntent.addCategory(Intent.CATEGORY_HOME);
                homeIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(homeIntent);
            }
        };

        requireActivity().getOnBackPressedDispatcher().addCallback(this, callback);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_attraction_test_2, container, false);
    }
    @SuppressLint({"InflateParams", "LongLogTag"})
    @Override
    public void onViewCreated(@NonNull final View view, @Nullable Bundle savedInstanceState) {
        createExampleList();
        buildRecyclerView();

        LinearLayout dialog_button = requireView().findViewById(R.id.dialog_button);

        dialog_button.setOnClickListener(v -> {
            Log.d(TAG, "onClick: opening dialog");
            DialogSort dialog = new DialogSort();
            dialog.show(getFragmentManager(), "DialogSort");
        });

        SearchView searchView = requireView().findViewById(R.id.searchView);
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                adapter.getFilter().filter(newText);
                return false;
            }
        });
    }

    public void createExampleList() {
        exampleList = new ArrayList<>();
        exampleList.add(new ItemAttractions(R.mipmap.unnamed, R.drawable.ic_kid, "Baby островок", "Детский", "60₽", 0));
        exampleList.add(new ItemAttractions(R.mipmap.unnamed, R.drawable.ic_kid, "Виражи", "Детский", "80₽", 1));
        exampleList.add(new ItemAttractions(R.mipmap.unnamed, R.drawable.ic_kid, "Вокруг света", "Детский", "50₽", 2));
        exampleList.add(new ItemAttractions(R.mipmap.unnamed, R.drawable.ic_interactive, "5D кинотеатр", "Интерактивный", "120₽", 3));
    }

    public void buildRecyclerView() {
        mRec = requireView().findViewById(R.id.attraction_recycler);
        adapter = new AdapterAttractions(exampleList);
        mLayoutManager = new LinearLayoutManager(getContext());
        mRec.setLayoutManager(mLayoutManager);
        mRec.setAdapter(adapter);
    }

Может можно как-то передать private AdapterAttractions adapter; и private ArrayList<ItemAttractions> exampleList; в статичный DialogFragment?


Ответы (0 шт):