爲什麼在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