Android findViewById在片段中返回null
我正在用新的片段替换现有的片段,我可以看到我的视图,但是当在按钮上单击listener时,它返回null。我得到以下例外情况:Android findViewById在片段中返回null,android,android-layout,android-fragments,Android,Android Layout,Android Fragments,我正在用新的片段替换现有的片段,我可以看到我的视图,但是当在按钮上单击listener时,它返回null。我得到以下例外情况: ?:??: W/?(?): java.lang.NullPointerException ?:??: W/?(?): at com.biggu.shopsavvy.fragments.xxxxxxxx.onCreateView(xxxxxx.java:34) ?:??: W/?(?): at android.support.v4.app.FragmentMana
?:??: W/?(?): java.lang.NullPointerException
?:??: W/?(?): at com.biggu.shopsavvy.fragments.xxxxxxxx.onCreateView(xxxxxx.java:34)
?:??: W/?(?): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:870)
?:??: W/?(?): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1080)
?:??: W/?(?): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:622)
?:??: W/?(?): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1416)
?:??: W/?(?): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:420)
?:??: W/?(?): at android.os.Handler.handleCallback(Handler.java:615)
?:??: W/?(?): at android.os.Handler.dispatchMessage(Handler.java:92)
?:??: W/?(?): at android.os.Looper.loop(Looper.java:137)
?:??: W/?(?): at android.app.ActivityThread.main(ActivityThread.java:4745)
?:??: W/?(?): at java.lang.reflect.Method.invokeNative(Native Method)
?:??: W/?(?): at java.lang.reflect.Method.invoke(Method.java:511)
?:??: W/?(?): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
?:??: W/?(?): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
?:??: W/?(?): at dalvik.system.NativeStart.main(Native Method)
我不知道发生了什么事
OnCreateView上的代码:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.capture_card_phone_number, container, false);
mPhone = (AutoCompleteTextView) getActivity().findViewById(R.id.phone_number);
Button next = (Button) getActivity().findViewById(R.id.capture_phone_next);
next.setOnClickListener(this);
// next.setEnabled(false);
return view;
我还导入了com.big.xxxxxxx.R
提前谢谢你的帮助
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
savedInstanceState) {
View view = inflater.inflate(R.layout.capture_card_phone_number, container, false);
mPhone = (AutoCompleteTextView) getActivity().findViewById(R.id.phone_number);
Button next = (Button) view.findViewById(R.id.capture_phone_next);
next.setOnClickListener(this);
return view;
您必须在视图中调用findViewById,而不是在活动中调用。原因是在onCreateView中,片段视图尚未创建,因此返回null。尝试在onResume中执行此操作,它将返回以下视图:
@Override
public void onResume() {
super.onResume();
mPhone = (AutoCompleteTextView) getActivity().findViewById(R.id.phone_number);
Button next = (Button) getActivity().findViewById(R.id.capture_phone_next);
next.setOnClickListener(this);
}
我已经清理并重新生成了R.java.plz,在方法onCreateView()中添加代码,特别是在xxxxx.java类的第34行,并为您的片段添加xml布局谢谢您的回复。我在OnCreateView中扩展了片段的视图,这就是为什么我可以查看片段,但无法设置任何类型的侦听器,因为我的按钮似乎为空(即使我可以看到按钮),我知道,但是添加您的代码,特别是我在第一条注释中告诉您的,如果没有,我无法帮助您,我应该看到代码来检测你的空指针的问题在哪里,可能是你按钮的id,可能是R.java,我认为你已经导入了android.R,而不是com.yourpackage.rb,但是你的按钮在片段视图或活动视图中?它位于capture\u card\u phone\u number布局中?是否有理由在活动中的活动上使用findviewbyid,但在片段中的视图上使用findviewbyid?