NullPointerException异常时使用findViewById [英] NullPointerException when using findViewById

查看:113
本文介绍了NullPointerException异常时使用findViewById的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用findViewById方法,在这样一个片段:

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

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

请注意,输出,在XML声明。

Note that "output" is declared in XML.

这code运行的屏幕,但是当我跑我的应用程序,它给了我一个NullPointerException异常上没有任何错误:

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)

另外请注意,包名被隐藏。我不明白为什么会这样,因为我已经用同样的线路code几次,我code,这从来没有发生过的其他应用程序。

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.

请帮忙!

推荐答案

作为previous ANS由黑带getView(说)返回之前充气内onCreateView其返回null,原因可能是你没有夸大视图的视图实际使用

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);

尝试用这些行更换您的code

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..

这篇关于NullPointerException异常时使用findViewById的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆