onGetLayoutInflater() cannot be executed until the Fragment is attached to the FragmentManager.

简介: onGetLayoutInflater() cannot be executed until the Fragment is attached to the FragmentManager.

出现这个异常来自于在Fragment中动态添加一个布局,切换的时候崩溃


写法如下:


getLayoutInflater().inflate(R.layout.layout_footer...)

调用的是Activity的getLayoutInflater

这句代码原本是没有什么问题的,但是在Fragment中使用就有问题了。


解决办法:

换一种写法


LayoutInflater.from(getActivity()).inflate(R.layout.layout_footer,...)

所以,

在Activity中可以直接用getLayoutInflater().inflate的方式,

在Fragment中要用LayoutInflater.from(getActivity()).inflate。


image.png

目录
相关文章
|
3月前
|
Android开发
解决E/RecyclerView: No layout manager attached; skipping layout
解决E/RecyclerView: No layout manager attached; skipping layout
170 1
ResizeObserver loop completed with undelivered notifications
ResizeObserver loop completed with undelivered notifications
|
4月前
|
Web App开发 JavaScript
vue报错【解决方案】 [Violation] Added non-passive event listener to a scroll-blocking <some> event.
vue报错【解决方案】 [Violation] Added non-passive event listener to a scroll-blocking <some> event.
425 0
No adapter attached; skipping layout 原因、解决办法
No adapter attached; skipping layout 原因、解决办法
1059 0
|
Android开发 Kotlin
【错误记录】Android Studio 运行报错 ( There is not enough memory to perform the requested operation. )
【错误记录】Android Studio 运行报错 ( There is not enough memory to perform the requested operation. )
689 0
【错误记录】Android Studio 运行报错 ( There is not enough memory to perform the requested operation. )
|
Java Android开发
Fragment not attached to Activity 异常
Fragment not attached to Activity 异常