2

Why does calling findViewById(android.R.id.content).getRootView() in onPause or onStop return NPE but not when I call it in onCreate?

learner
  • 11,490
  • 26
  • 97
  • 169
  • 1
    My best guess- in onPause and onStop you're unattached from the window that is holding you. – Gabe Sechan Apr 26 '13 at 20:59
  • Try this two links: http://stackoverflow.com/questions/4486034/get-root-view-from-current-activity http://stackoverflow.com/questions/7776768/android-what-is-android-r-id-content-used-for – Steve Benett Apr 26 '13 at 21:05

2 Answers2

1

Here is the code of 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;
    }

it at least return the view itself.

After I tried logging findViewById(android.R.id.content), and findViewById(android.R.id.content).getRootView() in onStop, onPause, onCreate, onResume, it worked all fine. No NPE.

could you put your Activity code?

Gaston Flores
  • 2,457
  • 3
  • 23
  • 42
tjPark
  • 308
  • 1
  • 11
0

I wasn't able to reproduce this error on Android 4.2.2. You might be getting that NullPointerException from something else. By the way, findViewById(android.R.id.content) returns the rootview so getRootView() is usually ineffective.

Nimooli
  • 89
  • 6