为什么在onPause或onStop中调用findViewById(android.R.id.content).getRootView()
会返回NPE,但在我在onCreate中调用它时不会返回?findViewById(android.R.id.content).getRootView()nullPointerException
3
A
回答
1
这里是android.view.View.getRootView()
的代码:
public View getRootView() {
if (mAttachInfo != null) {
final View v = mAttachInfo.mRootView;
if (v != null) {
return v;
}
}
View parent = this;
while (parent.mParent != null && parent.mParent instanceof View) {
parent = (View) parent.mParent;
}
return parent;
}
它至少返回视图本身。
我试图登录findViewById(android.R.id.content)
后,和findViewById(android.R.id.content).getRootView()
在的onStop,的onPause,的onCreate,的onResume,它的工作一切正常。没有NPE。
你可以把你的活动代码?
0
我无法在Android 4.2.2上重现此错误。你可能会从其他东西获得NullPointerException。 顺便说一下,findViewById(android.R.id.content)
返回rootview因此getRootView()
通常是无效的。
相关问题
- 1. findViewById nullPointerException
- 2. 可以findViewById(android.R.id.content)为Snackbars返回null?
- 3. TextView findviewbyID NullpointerException
- 4. NullPointerException异常findViewById()
- 5. DialogFragment中的findViewById --NullPointerException
- 6. Android findViewById抛出NullPointerException异常
- 7. getRootView()不工作
- 8. 为特定布局调用findViewById()? NullPointerException on findViewById()
- 9. 从findViewById使用视图时出现NullPointerException
- 10. findViewById导致NullPointerException无明显原因
- 11. “FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS”影响getRootView()。getHeight()
- 12. 查看getRootView返回自己
- 13. Jelly Bean android.R.id.content已更改?
- 14. 当在onCreate中调用findViewById时发生NullPointerException
- 15. 从处理程序线程调用FindViewById - NullPointerException
- 16. 在使用findViewById时onCreate()中的NullPointerException - 之前使用了setContentView?
- 17. FindViewById/Butterknife.findById仅在API上抛出NullPointerException <23
- 18. 在onRestart中调用findViewById时偶尔会出现NullPointerException
- 19. 取得被图纸上使用getRootView
- 20. getSupportFragmentManager()的BeginTransaction()。添加(android.R.id.content,F)不工作
- 21. ID为android.R.id.content的ViewGroup总是FrameLayout?
- 22. findViewById undefined
- 23. FindViewById Loop
- 24. Error findViewById()
- 25. findViewById ClassCastExcpetion
- 26. findViewByID返回null
- 27. findViewById VS SETID
- 28. FindViewByID片段空
- 29. findViewByID上BaseAdapter
- 30. (FrameLayout)findViewById(...)null
我最好的猜测 - 在onPause和onStop你没有从持有你的窗口中连接。 – 2013-04-26 20:59:30
试试这两个链接:http://stackoverflow.com/questions/4486034/get-root-view-from-current-activity http://stackoverflow.com/questions/7776768/android-what-is-android-r -id-内容使用的换 – 2013-04-26 21:05:54