源码:
/**
* Return the {@link Context} this fragment is currently associated with.
*
* @throws IllegalStateException if not currently associated with a context.
* @see #getContext()
*/
@NonNull
public final Context requireContext() {
Context context = getContext();
if (context == null) {
throw new IllegalStateException("Fragment " + this + " not attached to a context.");
}
return context;
}
/**
* Return the {@link Context} this fragment is currently associated with.
*
* @see #requireContext()
*/
@Nullable
public Context getContext() {
return mHost == null ? null : mHost.getContext();
}
原因:在DialogFragment中下载apk安装包的回调中有安装功能,安装apk需要context,从requireContext获取,但是此时DialogFragment已经onDestroy了,失去了与Activity的绑定,因此抛出异常。
解决:传入application作为context安装时所需context