findViewById(android.R.id.content).getRootView()nullPointerException

findViewById(android.R.id.content).getRootView()nullPointerException,android,android-activity,android-lifecycle,rootview,Android,Android Activity,Android Lifecycle,Rootview,为什么在onPause或onStop中调用findViewById(android.R.id.content).getRootView()会返回NPE,但在onCreate中调用时不会返回NPE?以下是android.view.getRootView()的代码: 它至少返回视图本身 在我尝试在onStop、onPause、onCreate、onResume中记录findViewById(android.R.id.content)和findViewById(android.R.id.content

为什么在onPause或onStop中调用
findViewById(android.R.id.content).getRootView()
会返回NPE,但在onCreate中调用时不会返回NPE?

以下是
android.view.getRootView()的代码:

它至少返回视图本身

在我尝试在onStop、onPause、onCreate、onResume中记录
findViewById(android.R.id.content)
findViewById(android.R.id.content).getRootView()
之后,一切正常。没有NPE


您可以输入您的活动代码吗?

我无法在Android 4.2.2上重现此错误。您可能会从其他内容中获得NullPointerException。
顺便说一句,
findViewById(android.R.id.content)
返回根视图,因此
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;
    }