0

EDIT: Yes I've sort of re-posted this but if you read the two posts, they both point to different issues with (hopefully) the same cause. In my other question, a particular widget is null. Here, the entire getView funtion is returning null.

I've been working on this for hours now. Can't find any reason. I can't post the entire fragment here but the following should make it clear.

@BindView(R.id.acService) AutoCompleteTextView autocompleteService;
@BindView(R.id.acAddress) AutoCompleteTextView autocompleteAddress;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState)
{
    View view = inflater.inflate(R.layout.fragment_home, container, false);
    unbinder = ButterKnife.bind(this, view);

    initialize();
    loadSkillsData();

    return view;
}

private void initialize()
{
    context = getActivity();
    util = new Util(context);
    requestService = new RequestService();
    geoDataClient = Places.getGeoDataClient(context, null);

    autocompleteAdapter = new PlaceAutocompleteAdapter(context, geoDataClient, BOUNDS_ONTARIO, null);
    autocompleteAddress.setAdapter(autocompleteAdapter);

    mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.mapFragment);
    mapFragment.getMapAsync(this);
}

private void loadSkillsData()
{
    Realm realm = getRealm();
    UserModel user = realm.where(UserModel.class).findFirst();

    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(RestAPI.ENDPOINT)
            .addConverterFactory(MoshiConverterFactory.create())
            .build();
    RestAPI restApi = retrofit.create(RestAPI.class);
    Call<ResponseSkills> loginCall = restApi.getSkills(user.getServerUserId());
    loginCall.enqueue(new Callback<ResponseSkills>()
    {
        @Override
        public void onResponse(Call<ResponseSkills> call, final Response<ResponseSkills> response)
        {
            if (response.isSuccessful())
            {
                if (response.body().getStatus())
                {
                    skillList = response.body().getSkillList();
                    ArrayAdapter<SkillModel> skillAdapter = new ArrayAdapter<>(context, android.R.layout.simple_list_item_1, skillList);
AutoCompleteTextView acService = getView().findViewById(R.id.acService);
                    acService.setAdapter(skillAdapter);
                }
                else
                {
                    switch (response.body().getError())
                    {
                        default:
                            Toasty.error(context, response.body().getError());
                            break;
                    }
                }
            } else
            {
                Toasty.error(context, getString(R.string.toast_experienced_a_problem)).show();
            }
        }

        @Override
        public void onFailure(Call<ResponseSkills> call, Throwable t)
        {
            Toasty.error(context, getString(R.string.toast_experienced_a_problem)).show();
            t.printStackTrace();
        }
    });
}

Layout: XML

On the line AutoCompleteTextView acService = getView().findViewById(R.id.acService);, I get a NPE saying that getView is returning null.

Why is this that so?

Asim
  • 6,962
  • 8
  • 38
  • 61
  • can you post error log ? – Lucifer Apr 06 '18 at 11:43
  • 3
    Do not post same question twice https://stackoverflow.com/questions/49691490/view-null-inside-fragment. – ADM Apr 06 '18 at 11:43
  • 1
    same question twice. down voted – Manish Gupta Apr 06 '18 at 11:44
  • Perhaps your view hasn't finished inflating hence the NPE? Try attaching an onGlobalLayoutListener to your view and doing your view manipulation code in there. – Thomas Cook Apr 06 '18 at 11:45
  • @ThomasCook Oh wow that fixed it. Can you please tell me why the view is taking so long to inflate? – Asim Apr 06 '18 at 12:19
  • It's not really an issue with how long it takes, the issue is that you have no control over how long it takes or at what point the OS decides to do the layout of your view. There is a difference between inflating a view and having the view be fully measured out and ready to interact with. By adding the view tree observer, you sidestep this issue and have a guarantee that the view will be ready to interact with as the OS is calling back to let you know it's ready. – Thomas Cook Apr 06 '18 at 12:44
  • @ThomasCook If you could post this as an answer here: https://stackoverflow.com/questions/49691490/view-null-inside-fragment – Asim Apr 12 '18 at 21:33
  • @Asim - sure, I'll do that now – Thomas Cook Apr 13 '18 at 08:36

0 Answers0