I was navigating through my fragment and suddenly this error happen
java.lang.IllegalStateException: Fragment PesananFragment{3c77b29} (5987833e-384c-48a3-b41b-2d3d1ecad053)} not attached to a context.
at androidx.fragment.app.Fragment.requireContext(Fragment.java:805)
at id.vividi.ui.utama.PesananFragment$fetchPaymentData$1.invokeSuspend(PesananFragment.kt:107)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:56)
at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:571)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:738)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:678)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:665)
and this is my code causing this error
VolleySingleton.getInstance(requireContext()).addToRequestQueue(historyRequest)
This error always happened when I am navigating through this fragment I've tried using requireContext.applicationContext nothing happen, the error still exists.
This is because your fragment is listening to some events and these are fired before the fragment is attached to context.
Try to use
private Context context;
@Override
public void onAttach(Context context) {
super.onAttach(activity);
this.context= context;
}
@Override
public void onDetach() {
super.onDetach();
this.context= null;
}
and while using this context , add
if(context!=null)