findViewById is not defined by Fragment

Upvote
AutoCompleteTextView autoCompView = 
          (AutoCompleteTextView) findViewById(R.id.autocomplete_city);

Gives me an error

The method findViewById is undefined for the type CityFragment.

with:

public class CityFragment extends Fragment {

    public CityFragment() {}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.city,
                    container, false);

    AutoCompleteTextView autoCompView = 
                (AutoCompleteTextView) findViewById(R.id.autocomplete_city);

        autoCompView.setAdapter(
                       new PlacesAutoCompleteAdapter(this, R.layout.list_item)
                );

    return rootView;
    }
}

I basically just copied the code from https://developers.google.com/places/training/autocomplete-android

Any ideas why I get the error?

Andy Res

That is because indeed Fragment does not have such method findViewById(). Instead, you should use the rootView to access it.

AutoCompleteTextView autoCompView = 
                (AutoCompleteTextView)rootView.findViewById(R.id.autocomplete_city);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

findViewById is not defined by Fragment

From Dev

Using findViewById inside a Fragment

From Dev

FindViewByID Fragment Null

From Dev

NullPointerException on findViewById on fragment replacement

From Dev

findViewById for inactive Fragment tabs

From Dev

FindViewByID Fragment Null

From Dev

findViewById in Fragment returns Null

From Dev

FindViewbyId always returns Null in Fragment

From Dev

Android fragment - findViewById returns null

From Dev

View.findViewById in fragment class

From Dev

Radio Button findViewById within a Fragment

From Dev

findViewById returns null in fragment class

From Dev

Sherlock fragment findViewById() and getIntent() error

From Dev

Android - Fragment findViewById() always null?

From Dev

FindViewbyId always returns Null in Fragment

From Dev

findViewById returns null in fragment class

From Dev

Calling findViewById in fragment returns null

From Dev

findViewById of TextSwitcher returns null after replacing fragment

From Dev

findViewById of a view from the framelayout enclosing a fragment fails

From Dev

The method findViewById(int) is undefined for the type PostDetail (Fragment)

From Dev

android - findViewById NULL under using Fragment

From Dev

findViewById() Returns null from within a fragment - Android

From Dev

Cannot resolve method 'findViewById(int)' in Fragment

From Dev

How to access findViewById in Fragment view - Kotln

From Dev

The method findViewById() is undefined for the type Fragment02

From Dev

FindViewByID returns null on Fragment Navigation Drawer

From Dev

Cannot resolve method findViewByID(int) fragment java

From Dev

Reference findViewbyID from Fragment in Non-Fragment class

From Dev

what do i use instead of findViewById in onCreateView using fragment

Related Related

  1. 1

    findViewById is not defined by Fragment

  2. 2

    Using findViewById inside a Fragment

  3. 3

    FindViewByID Fragment Null

  4. 4

    NullPointerException on findViewById on fragment replacement

  5. 5

    findViewById for inactive Fragment tabs

  6. 6

    FindViewByID Fragment Null

  7. 7

    findViewById in Fragment returns Null

  8. 8

    FindViewbyId always returns Null in Fragment

  9. 9

    Android fragment - findViewById returns null

  10. 10

    View.findViewById in fragment class

  11. 11

    Radio Button findViewById within a Fragment

  12. 12

    findViewById returns null in fragment class

  13. 13

    Sherlock fragment findViewById() and getIntent() error

  14. 14

    Android - Fragment findViewById() always null?

  15. 15

    FindViewbyId always returns Null in Fragment

  16. 16

    findViewById returns null in fragment class

  17. 17

    Calling findViewById in fragment returns null

  18. 18

    findViewById of TextSwitcher returns null after replacing fragment

  19. 19

    findViewById of a view from the framelayout enclosing a fragment fails

  20. 20

    The method findViewById(int) is undefined for the type PostDetail (Fragment)

  21. 21

    android - findViewById NULL under using Fragment

  22. 22

    findViewById() Returns null from within a fragment - Android

  23. 23

    Cannot resolve method 'findViewById(int)' in Fragment

  24. 24

    How to access findViewById in Fragment view - Kotln

  25. 25

    The method findViewById() is undefined for the type Fragment02

  26. 26

    FindViewByID returns null on Fragment Navigation Drawer

  27. 27

    Cannot resolve method findViewByID(int) fragment java

  28. 28

    Reference findViewbyID from Fragment in Non-Fragment class

  29. 29

    what do i use instead of findViewById in onCreateView using fragment

HotTag

Archive