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

目录
相关文章
|
1月前
|
Android开发
解决E/RecyclerView: No layout manager attached; skipping layout
解决E/RecyclerView: No layout manager attached; skipping layout
66 1
ResizeObserver loop completed with undelivered notifications
ResizeObserver loop completed with undelivered notifications
|
前端开发
Warning: This synthetic event is reused for performance reasons.
Warning: This synthetic event is reused for performance reasons.
503 0
Warning: This synthetic event is reused for performance reasons.
No adapter attached; skipping layout 原因、解决办法
No adapter attached; skipping layout 原因、解决办法
1033 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. )
663 0
【错误记录】Android Studio 运行报错 ( There is not enough memory to perform the requested operation. )
My Lead route attach and detach issue
My Lead route attach and detach issue
88 0
My Lead route attach and detach issue
2015-03-17 current note creation logic in my task
2015-03-17 current note creation logic in my task
110 0
2015-03-17 current note creation logic in my task
ABAP maintenance view event handling
ABAP maintenance view event handling
140 0
ABAP maintenance view event handling
My task - how is inline creation implemented
Created by Wang, Jerry, last modified on Mar 07, 2016
105 0
My task - how is inline creation implemented
|
Java Android开发
Fragment not attached to Activity 异常
Fragment not attached to Activity 异常