Android 使用findViewById时出现NullPointerException
我在如下片段中使用findViewById方法:Android 使用findViewById时出现NullPointerException,android,Android,我在如下片段中使用findViewById方法: output = (TextView) getView().findViewById(R.id.output); 注意,“输出”是用XML声明的 此代码运行时屏幕上没有错误,但当我运行我的应用程序时,它会给我一个NullPointerException: 03-09 12:50:33.507 28500-28500/com.andrewq.ezcalc E/AndroidRuntime﹕ FATAL EXCEPTION: main P
output = (TextView) getView().findViewById(R.id.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)
还要注意,包名称是隐藏的。我不明白为什么会发生这种情况,因为我已经在我编写的其他应用程序中多次使用了类似的代码行,但这种情况从未发生过
请帮忙
getView()
返回您在onCreateView
内部膨胀的视图,这就是为什么它在onCreateView
内部返回null。您可以使用充气机返回的视图,也可以将所有findViewById
相关逻辑移到getView()
返回您在onCreateView
内充气的视图,这就是它在onCreateView
内返回空值的原因。您可以使用充气机返回的视图,也可以将所有findViewById
相关逻辑移到getView()
返回您在onCreateView
内充气的视图,这就是它在onCreateView
内返回空值的原因。您可以使用充气机返回的视图,也可以将所有findViewById
相关逻辑移到getView()
返回您在onCreateView
内充气的视图,这就是它在onCreateView
内返回空值的原因。您可以使用充气机返回的视图,也可以将所有findViewById
相关逻辑移动到内部,正如blackbelt getView()在前面的ans中所说的那样,返回您在onCreateView内部充气的视图,并返回null,因为您可能在实际使用之前没有充气视图
output = (TextView) getView().findViewById(R.id.output);
尝试用这些行替换代码
View view = inflater.inflate(R.layout.mlayout,null);
output = (TextView) view.findViewById(R.id.output);
这就是它解决我问题的方法。可能这也会对您有所帮助。如blackbelt getView()在前面的ans中所述,返回您在onCreateView中膨胀的视图,并返回null,因为您可能在实际使用之前没有膨胀视图
output = (TextView) getView().findViewById(R.id.output);
尝试用这些行替换代码
View view = inflater.inflate(R.layout.mlayout,null);
output = (TextView) view.findViewById(R.id.output);
这就是它解决我问题的方法。可能这也会对您有所帮助。如blackbelt getView()在前面的ans中所述,返回您在onCreateView中膨胀的视图,并返回null,因为您可能在实际使用之前没有膨胀视图
output = (TextView) getView().findViewById(R.id.output);
尝试用这些行替换代码
View view = inflater.inflate(R.layout.mlayout,null);
output = (TextView) view.findViewById(R.id.output);
这就是它解决我问题的方法。可能这也会对您有所帮助。如blackbelt getView()在前面的ans中所述,返回您在onCreateView中膨胀的视图,并返回null,因为您可能在实际使用之前没有膨胀视图
output = (TextView) getView().findViewById(R.id.output);
尝试用这些行替换代码
View view = inflater.inflate(R.layout.mlayout,null);
output = (TextView) view.findViewById(R.id.output);
这就是它解决我问题的方法。也许这对你也有帮助。多亏了布莱克贝尔的帮助,我才使它工作起来。以下是我所做的: 我将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);
很好 多亏了布莱克贝尔的帮助,我才使它运转起来。以下是我所做的: 我将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);
很好 多亏了布莱克贝尔的帮助,我才使它运转起来。以下是我所做的: 我将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);
很好 多亏了布莱克贝尔的帮助,我才使它运转起来。以下是我所做的: 我将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);
很好