在Fragment中,findViewById返回null

8

我正在用新的片段替换现有的片段,我能够看到我的视图,但是在按钮上设置点击监听器时,它返回null。我得到了以下异常:

?:??: 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

感谢您的帮助。


我已经清理并重新生成了R.java文件。 - Preethi
请在onCreateView()方法中添加代码,特别是在xxxxx.java类的第34行,还要为您的片段添加xml布局。 - Houcine
感谢您的回复。我在片段的OnCreateView中填充了视图,这就是为什么我能够看到该片段但无法设置任何类型的侦听器,因为我的按钮似乎为空(即使我可以看到该按钮)。 - Preethi
我知道,但请添加您的代码,特别是我在第一条评论中告诉您的内容。如果没有,我无法帮助您。我需要看到代码以检测空指针问题出现在哪里,可能是您的按钮ID,也可能是R.java文件。我认为您导入了android.R而不是com.yourpackage.R。 - Houcine
但是你的按钮在Fragment视图还是Activity视图中?它在capture_card_phone_number布局中吗? - ania
2个回答

24
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方法,而不是在你的活动上调用。

1
你在Activity中使用findViewById,但在Fragment中却在View上使用,这有什么原因吗? - user1549672

0
原因是在onCreateView中,Fragment的视图尚未创建,因此它返回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);   
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接