Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 796
Preethi Avatar asked Jul 08 '12 18:07

Preethi


People also ask

Why is findViewById returning NULL?

FindViewById can be null if you call the wrong super constructor in a custom view. The ID tag is part of attrs, so if you ignore attrs, you delete the ID.

What does findViewById return?

findViewById returns an instance of View , which is then cast to the target class. All good so far. To setup the view, findViewById constructs an AttributeSet from the parameters in the associated XML declaration which it passes to the constructor of View . We then cast the View instance to Button .


1 Answers

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.

like image 58
swordiApps Avatar answered Oct 14 '22 15:10

swordiApps