片段未附加到导航组件的上下文
Fragment not attached to context on Navigation Component
我正在浏览我的片段,突然发生了这个错误
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.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)
这是我导致此错误的代码
VolleySingleton.getInstance(requireContext()).addToRequestQueue(historyRequest)
这个错误总是在我浏览这个片段时发生
我已经尝试使用 requireContext.applicationContext 没有任何反应,错误仍然存在。
这是因为您的片段正在侦听某些事件,并且这些事件在片段附加到上下文之前被触发。
尝试使用
private Context context;
@Override
public void onAttach(Context context) {
super.onAttach(activity);
this.context= context;
}
@Override
public void onDetach() {
super.onDetach();
this.context= null;
}
并在使用此上下文时添加
if(context!=null)
我正在浏览我的片段,突然发生了这个错误
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.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)
这是我导致此错误的代码
VolleySingleton.getInstance(requireContext()).addToRequestQueue(historyRequest)
这个错误总是在我浏览这个片段时发生 我已经尝试使用 requireContext.applicationContext 没有任何反应,错误仍然存在。
这是因为您的片段正在侦听某些事件,并且这些事件在片段附加到上下文之前被触发。
尝试使用
private Context context;
@Override
public void onAttach(Context context) {
super.onAttach(activity);
this.context= context;
}
@Override
public void onDetach() {
super.onDetach();
this.context= null;
}
并在使用此上下文时添加
if(context!=null)