Ошибка при сортировки RecyclerView из DialogFragment
Есть RecyclerView с item, мне нужно чтобы по нажатию на LinerLayout, открывался DialogFragment и в нём был ImageView, по нажатию на ImageView сортировался мой RecyclerView. Как исправить ошибку?
Так как сделал я, не работает, при нажатии на ImageView, приложение крашиться с ошибкой:
java.lang.NullPointerException: Attempt to read from field 'java.lang.String[] com.example.test.Attraction.FragmentAttractionRecyclerView.mArraysNames' on a null object reference
at com.example.test.Attraction.DialogSort.sortArray_type(DialogSort.java:36)
at com.example.test.Attraction.DialogSort.lambda$onCreateView$0$DialogSort(DialogSort.java:29)
at com.example.test.Attraction.-$$Lambda$DialogSort$hNUzI3gzFGpf0diQdPCF44sgutU.onClick(Unknown Source:2)
Fragment в котором находиться RecyclerView и LinerLayout.
public class FragmentAttractionRecyclerView extends Fragment {
private static final String TAG = "FragmentAttractionRecyclerView";
private RecyclerView mRec;
public AdapterAttractions adapter;
public ArrayList<ItemAttractions> exampleList;
private RecyclerView.LayoutManager mLayoutManager;
public 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);
}
}
DialogFragment в котором находится ImageView по нажатию на которую должен сортироваться RecyclerView из FragmentAttractionRecyclerView.
public class DialogSort extends DialogFragment {
FragmentAttractionRecyclerView fragmentAttractionRecyclerView;
ImageView img;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.dialog_sort, container, false);
img = view.findViewById(R.id.img);
img.setOnClickListener(v -> {
{
sortArray_type();
sortArrayList_type();
}
});
return view;
}
private void sortArray_type() {
Arrays.sort(fragmentAttractionRecyclerView.mArraysNames);
fragmentAttractionRecyclerView.adapter.notifyDataSetChanged();
}
private void sortArrayList_type() {
Collections.sort(fragmentAttractionRecyclerView.exampleList, (o1, o2) -> o1.get_attraction_type().compareTo(o2.get_attraction_type()));
fragmentAttractionRecyclerView.adapter.notifyDataSetChanged();
}
}