Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
541 views
in Technique[技术] by (71.8m points)

android - findViewById returning null in fragment

I am replacing the existing fragment with new fragment and i am able to see my view but while setting on click listener on the button it returns null . I get the following exception :

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

I have no clue what is happening ?

The code on 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;

I have also imported com.big.xxxxxxx.R

Thanks in advance for your help

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
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;

You have to call findViewById on your view - not on your activity.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...