Andrew Quebe
Andrew Quebe

Reputation: 2293

NullPointerException when using findViewById

I am using the findViewById method in a fragment like this:

output = (TextView) getView().findViewById(R.id.output);

Note that "output" is declared in XML.

This code runs with no errors on the screen but when I run my app, it gives me a NullPointerException:

03-09 12:50:33.507  28500-28500/com.andrewq.ezcalc E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.hidden.hidden, PID: 28500
    java.lang.NullPointerException
            at com.hidden.hidden.hidden.onCreateView(Hidden.java:50)
            at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
            at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
            at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
            at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:472)
            at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:163)
            at android.support.v4.view.ViewPager.populate(ViewPager.java:1068)
            at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
            at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436)
            at android.view.View.measure(View.java:16497)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
            at android.view.View.measure(View.java:16497)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
            at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:327)
            at android.view.View.measure(View.java:16497)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
            at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291)
            at android.view.View.measure(View.java:16497)
            at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1916)
            at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1113)
            at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1295)
            at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
            at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5670)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
            at android.view.Choreographer.doCallbacks(Choreographer.java:574)
            at android.view.Choreographer.doFrame(Choreographer.java:544)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)

Also note that package names are hidden. I don't understand why this is happening because I've used the similar line of code several times in other apps that I code and this has never happened.

Please help!

Upvotes: 0

Views: 1781

Answers (3)

Andrew Quebe
Andrew Quebe

Reputation: 2293

Thanks to blackbelt's help, I got it working. Here's what I did:

I had the onCreateView declared like so:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
   savedInstanceState) {
        View v = inflater.inflate(R.layout.layout, container, false);
   ...
   output = (TextView) getView().findViewById(R.id.output);
   ...
}

I got rid of getView(). and replaced it with this:

output = (TextView) v.findViewById(R.id.output);

Works great!

Upvotes: 0

Ravinder Bhandari
Ravinder Bhandari

Reputation: 2636

As said in previous ans by blackbelt getView() returns the view you inflate inside onCreateView and its returning null because may be you are not inflating view before actually using

output = (TextView) getView().findViewById(R.id.output);

try replacing your code with these lines

View view = inflater.inflate(R.layout.mlayout,null);
output = (TextView) view.findViewById(R.id.output);

this is how it solved my prob. may be it would be helpful for you too..

Upvotes: 2

Blackbelt
Blackbelt

Reputation: 157487

getView() returns the view you inflate inside onCreateView that's why it is returning null inside onCreateView. You can use the View returned by the inflater or move all the findViewById related logic inside onViewCreated

Upvotes: 1

Related Questions