There was a very strange error, which I cannot explain in any way. I have a Fragment in which there is a RecyclerView with a list, by clicking on one of the components, a transition to a new fragment occurs and using bundle.putString andsetArguments (bundle)I transfer data with a key to the newly created one fragment, then in this new fragment using Bundle bundle = getArguments (); I get the data that I sent from the Adapter and insert it into the TextView using .setText (Objects.requireNonNull(bundle).getString("SearchName")). And everything works absolutely fine, but I need to pass one line to CollectionReference or, more precisely, to .document(), since the list does not yet know which component was clicked and which collection it will need to display. And so, at first glance, everything works, but when the screen is rotated, the fragment is recreated and the application crashes with an error that .document() cannot be null, although it is probably not null, help me understand and fix the error.
At first, I thought that maybe the string really does not save the passed argument after rotating the screen, but I created a fragment with one TextView and tried to rotate the screen, everything works. So if you follow this logic, the problem is in СollectionReference. For some reason, it is he who does not save the passed argument after the screen rotation is a string.
Fragment
public class FragmentKeysBlanc extends Fragment { public static FragmentKeysBlanc newInstance() {return new FragmentKeysBlanc();} @Override public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);} @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view; view = inflater.inflate(R.layout.fragment_keys_blanc, container, false); return view;} @Override public void onViewCreated(@NonNull final View view, Bundle savedInstanceState) { Bundle bundle = getArguments(); FirebaseFirestore firebaseFirestore = FirebaseFirestore.getInstance(); CollectionReference collectionReference = firebaseFirestore.collection("Items").document(Objects.requireNonNull(bundle).getString("SearchName")).collection("TheKeyOpens"); Query query = collectionReference.orderBy("TurnTheKeyOpens", Query.Direction.DESCENDING); FirestoreRecyclerOptions<ItemTheKeyOpens> firestoreRecyclerOptions = new FirestoreRecyclerOptions.Builder<ItemTheKeyOpens>() .setQuery(query, ItemTheKeyOpens.class).build(); adapterTheKeyOpens = new AdapterTheKeyOpens(getContext(), firestoreRecyclerOptions); rv_the_key_opens = requireView().findViewById(R.id.rv_the_key_opens); RLMTheKeyOpens = new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false); SnapHelper snapHelper = new GravitySnapHelper(Gravity.START); snapHelper.attachToRecyclerView(rv_the_key_opens); rv_the_key_opens.setHasFixedSize(false); rv_the_key_opens.setLayoutManager(RLMTheKeyOpens); rv_the_key_opens.setNestedScrollingEnabled(true); rv_the_key_opens.setAdapter(adapterTheKeyOpens); adapterTheKeyOpens.startListening(); Objects.requireNonNull(rv_the_key_opens.getAdapter()).setStateRestorationPolicy(RecyclerView.Adapter.StateRestorationPolicy.PREVENT_WHEN_EMPTY); tv_keys_title.setText(Objects.requireNonNull(bundle).getString("SearchName")); } } The same mistake which says that .document () cannot be null
Process: com.example.eft_guides, PID: 15611 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.eft_guides/com.example.eft_guides.MainActivity}: java.lang.NullPointerException: Provided document path must not be null. at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2841) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2902) at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4765) at android.app.ActivityThread.-wrap19(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1623) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6397) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:939) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:829) Caused by: java.lang.NullPointerException: Provided document path must not be null. at com.google.firebase.firestore.util.Preconditions.checkNotNull(Preconditions.java:147) at com.google.firebase.firestore.CollectionReference.document(CollectionReference.java:103) at com.example.eft_guides.Menu.Keys.FragmentKeysBlanc.onViewCreated(FragmentKeysBlanc.java:170) at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:892) at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManagerImpl.java:1238) at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:1303) Adapter where I get data from cloud firestore, then give them a key and send it to Bundle
public class AdapterCustomsKeysSnap extends FirestoreRecyclerAdapter<ItemKeys, AdapterCustomsKeysSnap.HolderTasksTest> { private final Context context; private ClickKeysBlanc clickKeysBlanc; public AdapterCustomsKeysSnap(ClickKeysBlanc clickKeysBlanc , Context context, @NonNull FirestoreRecyclerOptions<ItemKeys> options) { super(options); this.clickKeysBlanc = clickKeysBlanc; this.context = context; } @Override protected void onBindViewHolder(@NonNull HolderTasksTest holder, int position, @NonNull ItemKeys model) { holder.tv_item_keys.setText(model.getNameKeys()); holder.tv_item_keys_number_of_uses.setText(model.getNumberOfUsesKeys().replace("Not limited", "")); Bundle bundle = new Bundle(); bundle.putString("SearchName", model.getSearchName()); MainActivity.curFragment.setArguments(bundle); holder.cv_item_keys.setOnClickListener(v -> { clickKeysBlanc.ClickKeysBlanc(); }); } @NonNull @Override public HolderTasksTest onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_keys_snap, parent, false); return new HolderTasksTest(view); } static class HolderTasksTest extends RecyclerView.ViewHolder { CardView cv_item_keys; ImageView iv_item_keys_1; ImageView iv_item_keys_2; TextView tv_item_keys, tv_item_keys_number_of_uses; public HolderTasksTest(View itemView) { super(itemView); cv_item_keys = itemView.findViewById(R.id.cv_item_keys); iv_item_keys_1 = itemView.findViewById(R.id.iv_item_keys_1); iv_item_keys_2 = itemView.findViewById(R.id.iv_item_keys_2); tv_item_keys = itemView.findViewById(R.id.tv_item_keys); tv_item_keys_number_of_uses = itemView.findViewById(R.id.tv_item_keys_number_of_uses); } } } https://stackoverflow.com/questions/67249374/getarguments-is-not-passed-on-screen-rotation April 25, 2021 at 11:03AM
没有评论:
发表评论