使用findViewById时出现NullPointerException

时间:2014-03-09 19:56:43

标签: android

我在像这样的片段中使用findViewById方法:

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

请注意,“output”以XML格式声明。

此代码在屏幕上运行时没有错误,但是当我运行我的应用程序时,它会给我一个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)

另请注意,包名称是隐藏的。我不明白为什么会发生这种情况,因为我在其他应用程序中使用了类似的代码行,而且这种情况从未发生过。

请帮忙!

3 个答案:

答案 0 :(得分:2)

如前所述,blackbelt中的getView()返回你在onCreateView里面膨胀的视图,并返回null,因为你可能在实际使用之前没有膨胀视图

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

尝试用这些行替换你的代码

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

这就是它如何解决我的问题。可能对你也有帮助..

答案 1 :(得分:1)

getView()会返回您在onCreateView内展开的视图,这就是它在onCreateView内返回null的原因。您可以使用inflater返回的视图或移动onViewCreated内的所有findViewById相关逻辑

答案 2 :(得分:0)

感谢blackbelt的帮助,我得到了它的工作。这是我做的:

我将onCreateView声明为:

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

我摆脱了getView()。并将其替换为:

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

效果很好!